Getting the coordinates of the "text" bounding box of a grayscaled picture by using command line in linux - linux

Just what the title says .
Strictly speaking what I define as "text" bounding box for a grayscaled image is a set of 4 coordinates (x,y,x+width,y+height) that have to define a rectangle area in that image that has the maximum number of non white pixels and at the same time the least possible number of white pixels(without chagning the maximum amount of non-white pixel). I have text in quotation marks since images does not actually contain text because images do only contain pixels with colours.
Having installed ImageMagick in my Ubuntu and typing in the terminal the command: $convert input.png -trim ouput.png , I get :
Open the two images in new tabs in your web browser and you will understand the difference they have and you will also understand what I define as "text" bounding box.
The output.png has actually the width and height that I am looking for.I do not know how to get x and y coordinates.
The answer provided here (1) for pdf pages does not meet my criteria since the "text" bounding box that gs gives me has big white margins ( and actually as far as I can understand what gs defines as "text" bounding box for a pdf is something different from my definition of "text" bounding box for a picture).

I don't understand all the words in your description, and I think a diagram would help, but if you just want to know what -trim would do as your sample code implies:
identify -format "%#" image.png
200x100+10+20
So, for your image, you get
identify -format "%#" paper.png
406x620+38+68
which means that your box is 38 pixels to the right of the top left corner and 68 pixels down from the top left corner, and it is 406 pixels wide and 620 pixels tall.
And if I draw in that rectangle in red, I get:
convert paper.png -stroke red -fill none -draw "rectangle 38,68 444,688" result.png
An alternative way of getting the same result but using convert in place of identify is:
convert -format %# paper.png info:
406x620+38+68

Images don't have a 'text bounding box', because obviously there is no text.
The images in the PDF file may themselves contain white pixels, if they are scanned from books then they almost certainly will. These pixels count towards the bounding box of the image, because they are white not transparent and will obscure anything drawn beneath them.
Its also rather nonsensical to define a 'text bounding box' as 'an area in that picture that has no white margins and only text'. If its in an image then there is no text, only image samples which define pixels. That's a picture of text, not actually text. In order to differentiate between areas of an image containing text and areas containing non-text you will need OCR software, nothing else is going to do this because only OCR software is capable of detecting the difference between text and non-text.

Related

Word Bounding Boxes of Azure OCR results are shifted to the left?

I am using the Azure OCR form recognizer to perform OCR. When I draw the line bounding boxes, it works great, but when I use the word bounding boxes, they are slightly shifted to the left.
For example, the line bounding boxes (ignore the red box) would look like this:lineocr
But when I draw the word bounding boxes from the same OCR results the result is shifted as follows:wordocr
Would anyone happen to know a solution for this problem, or maybe a nice workaround?
I have tried shifting the box by a certain percentage of the width of the bounding box but I would prefer to get the correct bounding box. The line bounding boxes have correct edges and I would expect the words to have them as well.

Remove all but certain colors (color range) in an image?

I have a grayscale image, which has a certain wanted pattern of colours + some unwanted colors.
The colors I want to leave are (rgb):
000
666
121212
181818
242424
303030
363636
424242
As you can see, there's a pattern. Umfortunately there are some really "hard to select" colors also. The colors are actually contours (curves for height changes of the terrain), so they would be really tiresome to edit manually. The image is 2048 x 2048 pixels.
How could I somehow limit the image just to have those certain colors and maybe even automagically convert the wrong ones to those wanted ones?
You could try converting the image mode to indexed color:
Then choose a custom pallet:
Then add your custom colors to the pallet. Set dither to none. It should convert all colors to the index color they are closest to.
To remove the white lines... convert mode back to rgb, use magic wand to select all white lines (uncheck contiguous and set tolerance to around 10). Then choose select>modify>expand and expand selection by 1 pixel. Now choose edit>fill and choose content-aware.
This should fill in the lines with the colors around them.
Set color to index again to clean up the blured shape edges.
Hope this helps.

Tinting Drawn Textures with the Color property in XNA / Monogame, how to balance?

I have these four icons that look like this:
But I want to be able to just have 1 gray icon and then tint them different colors by setting the color in XNA / Monogames Draw function.
I took one of the images and pulled all the colour out and made it a bit lighter it to get
I also tried a version where the center of the gray image is fully white but neither look right in the game, here's an image with the icons on top and my tinted gray on the bottom (the tint color is sampled from the center of the original icons):
I figure there should be a way, given the original icons, to figure out how to make the gray icon and also the colors to plug into the draw function so that I get an exact copy of the original. I assume this is possible? How do you do it?
EDIT:
To elaborate on my issue here is another image all with blue icons, the top images are all the original blue icon for comparison. The bottom images are all grey icons I used to tint to make the middle icons. The grey icons are, from left to right, 0 saturation, 0 saturation and 100 brightness, and auto level + 0 saturation. It seems to me that by setting the saturation to 0 I am losing the ability to make the icon look right as it's not just "darker" blue but also "bluer" blue.
EDIT 2: I don't think it's possible, thanks for the help Goose.
Would it work if the grey icon was full white at the center (it's brightest point) and got darker from there.
Then you could just sample the color at the center of your source image (the colored ones) and tint with that.
I think the issue you are seeing now is that the grey image will darken the color you are tinting with, so you actually want to tint with the source color PLUS the amount of grey at the brightest point in the image (if that makes sense).

Change pixels color [duplicate]

I have more then 1 week reading about selective color change of an image. It meand selcting a color from a color picker and then select a part of image in which I want to change the color and apply the changing of color form original color to color of color picker.
E.g. if I select a blue color in color picker and I also select a red part in the image I should be able to change red color to blue color in all the image.
Another example. If I have an image with red apples and oranges and if I select an apple on the image and a blue color in the color picket, then all apples should be changing the color from red to blue.
I have some ideas but of course I need something more concrete on how to do this
Thank you for reading
As a starting point, consider clustering the colors of your image. If you don't know how many clusters you want, then you will need methods to determine whether to merge or not two given clusters. For the moment, let us suppose that we know that number. For example, given the following image at left, I mapped its colors to 3 clusters, which have the mean colors as shown in the middle, and representing each cluster by its mean color gives the figure at right.
With the output at right, now what you need is a method to replace colors. Suppose the user clicks (a single point) somewhere in your image, then you know the positions in the original image that you will need to modify. For the next image, the user (me) clicked on a point that is contained by the "orange" cluster. Then he clicked on some blue hue. From that, you make a mask representing the points in the "orange" cluster and play with that. I considered a simple gaussian filter followed by a flat dilation 3x5. Then you replace the hues in the original image according to the produced mask (after the low pass filtering, the values on it are also considered as a alpha value for compositing the images).
Not perfect at all, but you could have a better clustering than me and also a much-less-primitive color replacement method. I intentionally skipped the details about clustering method, color space, and others, because I used only basic k-means on RGB without any pre-processing of the input. So you can consider the results above as a baseline for anything else you can do.
Given the image, a selected color, and a target new color - you can't do much that isn't ugly. You also need a range, some amount of variation in color, so you can say one pixel's color is "close enough" while another is clearly "different".
First step of processing: You create a mask image, which is grayscale and varying from 0.0 to 1.0 (or from zero to some maximum value we'll treat as 1.0), and the same size as the input image. For each input pixel, test if its color is sufficiently near the selected color. If it's "the same" or "close enough" put 1.0 in the mask. If it's different, put 0.0. If is sorta borderline, put an in-between value. Exactly how to do this depends on the details of the image.
This might work best in LAB space, and testing for sameness according to the angle of the A,B coordinates relative to their origin.
Once you have the mask, put it aside. Now color-transform the whole image. This might be best done in HSV space. Don't touch the V channel. Add a constant to S, modulo 360deg (or mod 256, if S is stored as bytes) and multiply S by a constant chosen so that the coordinates in HSV corresponding to the selected color is moved to the HSV coordinates for the target color. Convert the transformed S and H, with the unchanged L, back to RGB.
Finally, use the mask to blend the original image with the color-transformed one. Apply this to each channel - red, green, blue:
output = (1-mask)*original + mask*transformed
If you're doing it all in byte arrays, 0 is 0.0 and 255 is 1.0, and be careful of overflow and signed/unsigned problems.

Convert a black & white image to white & transparent

I have a black and white System.Drawing.Bitmap being passed from a 3rd party SDK, that I want to convert from black on white to white on transparent... i.e. make all the black pixels white, and all the white pixels transparent.
Do I have to iterate over every pixel, check the color and change it accordingly? How do I go about this?
If it's a paletted image (as it should be, given that it's a BW image) you could replace black with transparent inside the palette of the bitmap (Palette property of Bitmap, edit its Entries property).

Resources