how to generate pixel data of an image - image-processing

I'm working on a video encoder having the part number ADV7391BCPZ. Now i have to generate an image using encoder. The FPGA is feeding the required input to encoder. I want to generate the pixel data of the image interms of Y Cr Cb format because, my encoder will accept only that form of input.
Now i request you to suggest the method to convert an image to its pixel data.

The very simplest way to do that is with ImageMagick in your Terminal:
magick z9mSc.jpg -colorspace YCC txt:
Sample Output
# ImageMagick pixel enumeration: 180,180,255,ycc
0,0: (255,156.098,136.955) #FF9C89 ycc(255,156.098,136.955)
1,0: (255,156.098,136.955) #FF9C89 ycc(255,156.098,136.955)
2,0: (255,156.098,136.955) #FF9C89 ycc(255,156.098,136.955)
3,0: (255,156.098,136.955) #FF9C89 ycc(255,156.098,136.955)
4,0: (255,156.098,136.955) #FF9C89 ycc(255,156.098,136.955)
...
...
That tells you pixel 0,0 has YCC values (255,156.098,136.955) followed by the other pixels.
Or, if you want it in binary, you can do:
magick z9mSc.jpg YCbCr:image.bin
which will give you a file called image.bin with size 97,200 bytes because your image is 180x180 pixels and you will get 3 bytes (Y, Cb and Cr) for each pixel in the binary file.
Note that you can convert your image into its three constituent channels and lay them out side-by-side with Y on the left, Cb in the middle and Cr on the right like this:
magick z9mSc.jpg -colorspace YCBCR -separate +append separated.png
If you do that, you will notice that all the information is in the Y channel and that the other two channels are constant. So, you could save space and just use the Y channel in your FPGA and synthesise constant Cb and Cr channels alongside it. To extract just the Y channel, you can do:
magick z9mSc.jpg -colorspace YCBCR -channel R -separate gray:Y.bin

As you seem to be having trouble using ImageMagick, another option might be to use ffmpeg like this:
ffmpeg -i z9mSc.jpg -f rawvideo -pix_fmt yuv444p image.yuv
which gives you a planar YUV file of 97,200 bytes which seems correct for your 180x180 image.

Related

sort images based on their color

I have some images from stones. Stone colors are creamy and I want to classify images based on their difference in color.I want to give the lightest stone grade 0 and to the darkest stone grade 10 and classify others between these two. Stones are very similar but their color difference is detectable by eyes.
I know if stones where all blue for example I could classify them based on R part of RGB color. But what about creamy color?
You don't need to go to the complexity of installing a compiler and OpenCV and writing/compiling Python/C++ code to get the mean lightness of your images. You can just use ImageMagick which is installed on most Linux distros and is available for macOS and Windows too.
Basically, you could look at the "Lightness" in HSL colourspace or in the Lab colourspace.
Let's look at HSL first.
# Resize stone down to one average pixel, convert to HSL colourspace and print
convert stone1.jpg -resize 1x1 -colorspace HSL txt:
# ImageMagick pixel enumeration: 1,1,65535,hsl
0,0: (4228.92,19250.9,52587.2) #104BCD hsl(23.2305,29.375%,80.2429%)
So the lightness of stone1.jpg in HSL is 80.24%. Let's try stone2.jpg:
convert stone2.jpg -resize 1x1 -colorspace HSL txt:
# ImageMagick pixel enumeration: 1,1,65535,hsl
0,0: (7387.85,27252.5,57243) #1D6ADF hsl(40.5833,41.5846%,87.3472%)
So, stone2.jpg is lighter at 87.35%
Let's simplify the output to show only the Lightness:
convert stone1.jpg -colorspace HSL -format "%[fx:int(100*mean.b)]" info:
80
and stone2.jpg:
convert stone2.jpg -colorspace HSL -format "%[fx:int(100*mean.b)]" info:
87
Let's look at Lab colourspace now.
convert stone1.jpg -resize 1x1 -colorspace Lab txt:
# ImageMagick pixel enumeration: 1,1,65535,cielab
0,0: (53895.2,1140.43,2057.36) #D20408 cielab(82.2388%,1.74018%,3.13933%)
So, stone1.jpg has a Lab Lightness of 82.24%, let's look at stone2.jpg:
convert stone2.jpg -resize 1x1 -colorspace Lab txt:
# ImageMagick pixel enumeration: 1,1,65535,cielab
0,0: (59395,-21.0391,2545.27) #E7000A cielab(90.6309%,-0.0321036%,3.88383%)
So, stone2.jpg has a lightness of 90.6%.
What if we want the simpler form?
convert stone1.jpg -colorspace Lab -format "%[fx:int(100*mean.r)]" info:
82
What are the percentages of? They are the percentage white, so pure white would be 100% and pure black would be 0%. Quick test...
convert xc:black -colorspace Lab txt:
# ImageMagick pixel enumeration: 1,1,65535,cielab
0,0: (0,-0.5,-0.5) #000000000000 cielab(0%,-0.000762951%,-0.000762951%)
convert xc:white -colorspace Lab txt:
# ImageMagick pixel enumeration: 1,1,65535,cielab
0,0: (65535,0.125,-1.69336) #FFFF00000000 cielab(100%,0.000190738%,-0.0025839%)
What if you have a whole directory full of stone samples and you want the HSL Lightness values for each?
convert stone* -colorspace HSL -format "%f:%[fx:int(100*mean.b)]\n" info:
stone1.jpg:80
stone2.jpg:87
stone3.jpg:75
stone4.jpg:92
I want to classify images based on their difference in color.I want to give the lightest stone grade 0 and to the darkest stone grade 10 and classify others between these two. Stones are very similar but their color difference is detectable by eyes.
If you want to order them from light to dark, and take into account the hue, the RGB colour space is not well suited. You should have a look at the Lab colour space or maybe the Hue Saturation Lightness (HSL) colour space.
By converting the images into one of these colour spaces, and then taking the average, you can then sort into lightness, and one or two axes of colour/hue. (This is assuming the image content is mostly homogenous.)

get the most representative colour from the uppermost pixel of a picture (the sky colour)

Using any language readily available under a UNIX system, I'd like to get the the representative RGB colour values for the uppermost pixels of a JPG file.
In other words, given an image file, I'd like to -crop3072x1, resize to 1x1 (obviously without preserving the aspect ratio), then get the RGB value of the resulting pixel.
Using GraphicsMagick, there's apparently a TXT image format that's available:
gm convert IMG_X.JPG -crop x1 -resize 1x1\! txt:-
For example:
% gm convert IMG_7753.JPG -crop 2800x1 -resize 1x1\! txt:-
0,0: ( 63,107,158) #3F6B9E
References:
http://www.graphicsmagick.org/convert.html
http://www.graphicsmagick.org/GraphicsMagick.html#details-crop
http://www.graphicsmagick.org/GraphicsMagick.html#details-resize
http://www.graphicsmagick.org/GraphicsMagick.html#details-geometry

How to treshold image from greyscale screen by webcome

I have image like this from my windstation
I have tried get thoose lines recognized, but lost becuase all filters not recognize lines.
Any ideas what i have use to get it black&white with at least some needed lines?
Typical detection result is something like this:
I need detect edges of digit, which seams not recognized with almost any settings.
This doesn't provide you with a complete guide as to how to solve your image processing question with opencv but it contains some hints and observations that may help you get there. My weapon of choice is ImageMagick, which is installed on most Linux distros and is available for OS X and Windows.
Firstly, I note you have date and time across the top and you haven't cropped correctly at the lower right hand side - these extraneous pixels will affect contrast stretches, so I crop them off.
Secondly, I separate your image in 3 channels - R, G and B and look at them all. The R and B channels are very noisy, so I would probably go with the Green channel. Alternatively, the Lightness channel is pretty reasonable if you go to HSL mode and discard the Hue and Saturation.
convert display.jpg -separate channel.jpg
Red
Green
Blue
Now make a histogram to look at the tonal distribution:
convert display.jpg -crop 500x300+0+80 -colorspace hsl -separate -delete 0,1 -format %c histogram:png:ahistogram.png
Now I can see all your data are down the dark, left-hand end of the histogram, so I do a contrast stretch and a median filter to remove the noise
convert display.jpg -crop 500x300+0+80 -colorspace hsl -separate -delete 0,1 -median 9x9 -normalize -level 0%,40% z.jpg
And a final threshold to get black and white...
convert display.jpg -crop 500x300+0+80 -colorspace hsl -separate -delete 0,1 -median 9x9 -normalize -level 0%,40% -threshold 60% z.jpg
Of course, you can diddle around with the numbers and levels, but there may be a couple of ideas in there that you can develop... in OpenCV or ImageMagick.

Changing exposure of jpeg

Given a jpeg, what is the formula to change the exposure of that jpeg by +/-1 stop or as known as 1 EV? I want to simulate this exposure change. Is there a formula/ method to do so?
I can demonstrate that using ImageMagick, which is included in most Linux distros and available for OSX and Windows from here.
First, at the Terminal command line create an image:
convert -size 512x512 gradient:black-yellow gradient.png
Now, the way to effect +1 stop exposure increase is to composite the image with itself using the Screen blending mode - it is available in Photoshop and ImageMagick and is described here.
So, the formula to composite image A with image B is:
1-stop brighter image = 1-(1-A)(1-B)
but as we are compositing the image with itself, A and B are the same, so we effectively have
1-(1-A)(1-A)
ImageMagick refers to the pixels of an image using p rather than A, so we can do a 1-stop increase like this:
convert gradient.png -colorspace RGB -fx "(1-(1-p)(1-p))" result.png
Note that the Wikipedia article, and ImageMagick's -fx both assume your pixel intensities vary between 0 and 1.0. If you are using 8-bit images, you should calculate with 255 in place of 1, namely
+1 stop brighter image = 255-(255-A)(255-A)
or if using 16-bit values
+1 stop brighter image = 65535-(65535-A)(65535-A)
The above fx-based method is however, very slow because the -fx is interpreted rather than compiled, so a faster way to do it is:
convert gradient.png gradient.png -colorspace RGB -compose screen -composite screen.png
Just for fun, another way of looking at that is that we take the inverse of A, that is 1-A, and square it, and then take the inverse, so it can be done like this:
convert gradient.png -colorspace RGB -negate -evaluate pow 2 -negate result.png
The equivalent of -1 stop exposure decrease is to composite the image with itself using the Multiply blend mode, the formula being
1-stop darker image = A x B
which you would do faster with
convert gradient.png gradient.png -colorspace RGB -compose multiply -composite result.png
or even faster, by using memory-to-memory cloning rather than reading from disk twice, with
convert gradient.png -colorspace RGB +clone -compose multiply -composite result.png
but could do equally with
convert gradient.png -colorspace RGB -evaluate pow 2 result.png

ImageMagick: write pixels in BGR order

I want to convert (a lot of) JPEG images to the Sun Raster format (see here for instance), because my program can only use this format.
The issue is that my program only allows images of type Old
or Standard i.e. with pixel written in the BGR order. (I should change that but don't have the time).
By default, ImageMagick generates files in the RGB format.
I have found a trick to swap the color planes, but as the file is style written as RGB I get fancy colors.
This is probably a simple option to pass to ImageMagick, but I didn't find it.
Do you have an idea?
Another way to swap colors is to use the -color-matrix option. For example, to convert RGB to BGR:
convert input.png -color-matrix '0 0 1
0 1 0
1 0 0' output.png
Reference: http://www.imagemagick.org/Usage/color_mods/
In Imagemagick 6, you can do that easily with -swap command. Lets swap the red and blue channels in the logo image.
convert logo.png -separate +channel \
-swap 0,2 -combine -colorspace sRGB logo_bgr.png
For very old versions of Imagemagick use RGB rather than sRGB.
In Imagemagick 7, use magick rather than convert.
I have an idea how to swap Red and Blue colors in RGB image:
convert ( image_RGB.bmp -channel B -separate ) \
( image_RGB.bmp -channel G -separate ) \
( image_RGB.bmp -channel R -separate ) -channel RGB -combine image_BGR.bmp
You just cut channels form original image and write them in reverse order.
I would have thought that in Imagemagick
convert image.jpg image.sun
or
convert image.jpg SUN:image.sun
would convert to BGR format if that is the standard for the sun raster format. See imagemagick.org/script/formats.php.
Have you tried that?
If that does not work, then use my -swap command but write to .sun file format rather than png, which I used above. That is
convert logo.png -separate +channel -swap 0,2 -combine -colorspace sRGB logo_bgr.sun

Resources