How to efficiently detect bar code target in an image - image

I have two questions:
Firstly, how to detect the area of bar code target in an image (like the sample images), which may have a few noises.
Secondly, how to efficiently do the detection, for instance, in 1/30 seconds.

Squash (resize) the image till it is only 1 pixel tall, then normalise it to the full range of 0-255 and threshold. I am using ImageMagick at the command-line here - it is installed on most Linux distros and is available for OSX and Windows also with Python, PHP, Ruby, C/C++ bindings.
convert barcode.png -resize x1! -scale x10! -normalize -threshold 50% result.png
I have then scaled it to 10 pixels tall so you can actually see it on here - but you would keep the original width and have a height of one pixel. Then just find the first white pixel in your single row of pixels.
Your recently added, smaller barcode gives this:

Related

converting a ps image to gif image with length x900 y800

I have a ps image that I want to convert to a gif image with horizontal by vertical dimensions of 900 and 800 respectively. I have tried to use the command:
convert panel.gs -resize x800 y900 panel.gif
or also:
convert panel.gs -resize 900x800 panel.gif
Can you help me to tweak the convert commands so I can get the desired results?
.gs is not a valid suffix. Did you mean .ps?
Imagemagick will need ghostscript as a delegate. You did not say what was wrong nor what platform or what version of Imagemagick.
If the image does not have the same aspect ratio as the final dimensions you want, you will either 1) need to distort it to fit using !, 2) resize it and then extend to the size you want filling with background color, or 3) resize it with ^ and crop it to the size you want.
convert panel.ps -resize "900x800!" panel.gif
convert panel.ps -resize 900x800 -gravity center -background white -extent 900x800 panel.gif
convert panel.ps -resize "900x800^" -gravity center -extent 900x800 panel.gif
Well, firstly you haven't actually said what's wrong with the two commands that you have tried already.....
Your PostScript program probably does not contain an 'image' as such, PostScript is not a bitmap format its a programming language.
You can use Ghostscript to render the PostScript to an image, and then use ImageMagick to resize that image, possibly you can combine these two steps, or just perform a single conversion, it depends on what exactly you want to happen, which isn't clear.
If (for example) your PostScript program requests a media size of 9 inches by 8 then you can create a bitmap image by simply setting the resolution to 100 dpi using -r100.
If you want the image scaled differently in each direction, then you need to set a non-square resolution. For example if the PostScript program requests media of 9 inches by 4 then you need to set the resolution to 100x200 in order to get an image exactly 900 x 800 pixels. You would use -r100x200 for this.
The alternative, from a PostScript point of view, is to set the media size to a given
value in pixels (using -g900x800) and set -dDFIXEDMEDIA which prevents the PostScript program from changing it. You can then use -dFitPage which will have Ghostscript scale the content to fit the page. However it will scale the content equally in both directions, which may leave white space around the edge.
Now since Ghostscritp doesn't write GIF directly you'll need to load whatever bitmap format you select into IM in order to write it out as a GIF, so perhaps the simplest solution is just to use Ghostscript to render the PostScript to a defined resolution (eg 100 dpi) and then load that image into IM and rescale it there.
Since IM (and therefore convert) use Ghostscript to process PostScript programs, that's what's happening now so it isn't obvious to me what your problem is.

What image metrics to use for selecting pictures for thumbnails?

I would like to automate process of creating thumbnails / contact sheets for videos. They are usually m x n matrixes of pictures, eg 6x11 or 8x12 etc. Randomly selected pictures are sometimes bad quality: contains movement (blurry image), camera spans (blurry too), too dark or completely black, or completely white, no details, etc. Currently I am using the jpg image file size for image metric: bigger file size -> more details on the picture. Combined with number of colors (can be determined with ImageMagick "identify -format %k" command). I normalize both to 0.0-1.0 interval by dividing with the largest value in the group of the pictures and then I compute the following metric:
gamma*number_of_colors^2+(1-gamma)*file_size^2
Where gamma is a weighting parameter and can be in interval 0.0-1.0. What other approaches, image metrics can be used for this purpose?
If you are interested in sharpness/blurriness, you could go to greyscale and run an edge detection (e.g. Canny) which will give you a generally black image with white areas where sharp edges are detected. If you take the mean brightness of such an image (or count the white pixels and divide by the image area in pixels), the ones that have a higher brightness are the ones with more sharp edges.
convert image.jpg -colorspace gray -canny 0x1+10%+30% -format "%[fx:mean]" info:
So, by way of example... using this sharp image:
I test the sharpness:
convert sharp.jpg -colorspace gray -canny 0x1+10%+30% -format "%[fx:mean]" info:
0.00485202
Now, with a blurry version:
I now get this:
convert blurred.jpg -colorspace gray -canny 0x1+10%+30% -format "%[fx:mean]" info:
0.00261855

How to categorize by color in Ruby like Dribbble?

Dribble has a great feature that lets you browse shots by similar colors:
What is the easiest way to generate something like this in Ruby? Are there libraries or services that can manage this kind of processing? I currently have 26k images which I'll need to process and I'm evaluating the best way to do so.
This will most likely rely on the imagemagick utility in some capacity. A quick search of available libraries turned up the Miro gem available here: https://github.com/jonbuda/miro.
Not sure which aspect of the problem you need help with - generating the swatches of colour or sorting by similar colours. Anyway, here is how you can use ImageMagick to generate the 6 best colours to represent an image and make that into a colour swatch of a resonable size:
convert input.png -colors 6 -unique-colors -scale 5000% swatch.png
If you want the colours as RGB triplets, just change the command to this:
convert input.png -colors 6 -unique-colors +matte -colorspace RGB txt:
# ImageMagick pixel enumeration: 6,1,255,rgb
0,0: (1623,1472,1531) #060606 rgb(6,6,6)
1,0: (10693,4106,4231) #2A1010 rgb(42,16,16)
2,0: (23082,8867,9471) #5A2325 rgb(90,35,37)
3,0: (8667,28247,37488) #226E92 rgb(34,110,146)
4,0: (40714,34524,37545) #9E8692 rgb(158,134,146)
5,0: (59611,58620,58816) #E8E4E5 rgb(232,228,229)
And if you want to find the distance between two colours, e.g. the first and last colours listed above, you can use this:
compare -metric RMSE xc:"rgb(232,228,229)" xc:"rgb(6,6,6)" null:
57484 (0.87715)
The number in parentheses means that the colour distance in the RGB colour cube is 87% of the distance between black and white - i.e. normalised to the diagonal of the colour cube as 100%. As the first number is nearly black, i.e. rgb(0,0,0) and the second is nearly white, i.e. rgb(255,255,255), the distance between the colours is 87%.
There are Ruby bindings for ImageMagick - here and here.

Imagemagick position and size of a sub-image

Problem description:
In imagemagick, it is very easy to diff two images using compare, which produces an image with the same size as the two images being diff'd, with the diff data. I would like to use the diff data and crop that part from the original image, while maintaining the image size by filling the rest of the space with alpha.
The approach I am taking:
I am now trying to figure the bounding box of the diff, without luck. For example, below is the script I am using to produce the diff image, see below. Now, I need to find the bounding box of the red color part of the image. The bounding box is demonstrated below, too. Note that the numbers in the image are arbitrary and not the actual values I am seeking.
compare -density 300 -metric AE -fuzz 10% ${image} ${otherImage} -compose src ${OUTPUT_DIR}/diff${i}-${j}.png
You asked quite a while ago - I found the question just today. Since I think the answer might still be of interest, I propose the following.
The trim option of convert removes any edges that are the same color as the corner pixels. The page or virtual canvas information of the image is preserved. Therefore, if you run
convert -trim edPdf.png - | identify -
it gives you:
PNG 157x146 512x406+144+32 8-bit PseudoClass 2c 1.08KB 0.000u 0:00.000
and the values you are looking for are (144,228), where the latter is 406-146-32 because you looked for the lower left corner whereas (+144+32) gives the upper left corner.

Imagemagick - Resize images to 25px height and aspect ratio

OK, so I have a folder of like 16 images all between the dimensions of 205x150 to 103x148. I want to size them down to the pixel height and width of 25px and stack them horizontally on a transparent background... is that possible?
I should probably be using ImageMagick for this...
You can do all that with ImageMagick.
You're question is not very specific, so here's a quick cheat sheet of command examples that may help you:
# resize image to width 25, keeping aspect ratio
convert -geometry 25x src/image1.png out/image1.png
# resize image to height 25, keeping aspect ratio
convert -geometry x25 src/image1.png out/image1.png
# concatenate images horizontally
convert +append src/image1.png src/image2.png out/image12horiz.png
# concatenate images vertically
convert -append src/image1.png src/image2.png out/image12vert.png
In addition, the montage command is probably perfect to create the final image you are looking for (on a transparent bg with some padding, etc), but I don't remember the syntax.
Another useful command is identify, to find the dimensions of images (along with other details).
After you install ImageMagick, you can see the list of commands in man ImageMagick, and get further details on each command in the man pages. There are an awful lot of functions, but it should not be too difficult to figure out the rest on Google. (I do that all the time.)
Just to add something to #janos answer.
I haven't used previous versions of ImageMagick but on version v6 or later according to the docs http://www.imagemagick.org/Usage/resize/#geometry
Geometry is a very special option. The operator behaves slightly differently in every IM command, and often in special and magical ways. The reasons for this is mostly due to legacy use and should be avoided if at all possible.
So other than the -geometry parameter you can still use -resize and omit the value you want in order to keep the aspect ratio. You can also use the -quality parameter to avoid image quality downgrade when resizing them. Value of quality is between 1 (lowest image quality and highest compression) and 100 (best quality but least effective compression). You can read more here: https://imagemagick.org/script/command-line-options.php#quality
For example:
# resize image to height 25, keeping aspect ratio with quality 90
convert -resize x25 original_image.jpeg -quality 90 resized_image.jpeg

Resources