White binary image in Matlab - image

in Matlab,
if I do:
output = false(5, 5);
imshow(output);
it will show me a black square instead of a white binary square image. Is there any reason to this? How can I output a white binary square?

The reason is that false is mapped to 0, and true is mapped to 1.
Also, when showing images, higher number is shown by higher intensity. White has more intensity than black.
Another way to think about it, is that usually you have 256 values - 0-255. 0 is totally black and 255 is totally white. Now, imagine that you do a quantization to two colors. It is now obvious that 0 should be black.
In order to show white square, use
output = true(5,5)

You could use imcomplement
imshow(imcomplement(false(5, 5)))
or modify the default color mapping (quoting from imshow's documentation)
imshow(X,map)
displays the indexed image X with the colormap map. A color map matrix may have any number of rows, but it must have exactly 3 columns. Each row is interpreted as a color, with the first element specifying the intensity of red light, the second green, and the third blue. Color intensity can be specified on the interval 0.0 to 1.0.

You could also change the figure's colormap to customize how MATLAB maps values to colors:
BW = [false,true;true,false];
imshow(BW)
set(gcf, 'Colormap',[1,1,1;0,0,0])

Related

Get pixel values from image: Matlab

I have a simple green fluorescent image. I want to find the total number of pixels that are above a specific value using MATLAB. I don't know where the pixel values are stored in an image.
Here is the green fluorescent image. I want to know which percentage of the pixels have value of more than a specific threshold. For example in this image, if the pixel value in the cells are around X, then I want to find the total number of pixels that are above X.
If you read a colored image using imread, you get a 3D matrix in which the first two indices are the image coordinates; (row, columns); and the last index represents the color channels. For the typical use case of an RGB image, the color channels are:
1 = red
2 = green
3 = blue.
Other possibilities are grayscale, CMYK and indexed images. Please check the official documentation for more information.

Background for indexed image

I have an indexed image (2-D, not rgb), and i use imagesc to display the image. This function gives a range from blue to red, which can be set by colormap, and can be viewed by colorbar.
Now, I want to change the background, that is usually 0's or NaN's, to white or black, but that it will not affect or change the range of the colormap/colorbar. I've tried converting all the image to 3-D rgb, but this prevent the option of changing the contrast, or the clims, like in imagesc.
So, there is other way to do that?
EDIT:
#Shai's solution was good, but it caused other problem-
When I have an image with a range of values and the background is NaN's, and I display the image in a specific contrast (by imagesc(img, [-1,1]) for example), I get range of colors between -1 to 1, and i get white in the background (because i put [1 1 1] in the first entry of the colormap), but also all the values under the minimum of contrast (i.e., <-1 in the example) also get the white color instead of the bottom dark blue in the colorbar scale.
Any idea for that...?
Many Thanks.
Consider putting [1, 1, 1] as the first entry of your colormap to get the background as white.

Matlab: How can I find the number of black pixels in a color image?

I wish to find the number of black pixels in a color image using MATLAB, how can I do it?
Thanks.
Use nnz like this -
nnz(all(im==0,3))
where im is your image data.
Alternatives - sum(reshape(all(im==0,3),[],1)) and sum(sum(all(im==0,3))).
The assumption here is that black pixels are triplets (0,0,0).
Instead if you define black pixels as pixels that have values in the interval [0 th] for the same pixel location across all channels, use this -
nnz(all(im<=th,3))

Selective Color of image

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.

is there any difference between grey scale image and binary image?

is there any difference between grey scale image and binary image?
Yes, the one is grayscale, e.g. gray scales from 0.255, the binary imange is binary, that means black(0) or white(1).
EDIT: Convert grayscale to binary. Directly converting color images (like RGB) to binary is not that easy, because you have to handle every color channel within the image seperatly.
Converting to binary is done using a ceratin threshold. E.g. you can say, all pixels with gray > 125 will become white, the others black.
There are several thresholding algorithm out there, but maybe the most common is Otsu. You can find it here Thresholding by Otsu
Yes, but I'm not sure what this has to do with C++ or programming. A binary image could be an image where pixels are only either red or blue.
Binary Image: http://en.wikipedia.org/wiki/Binary_image
Grayscale Image: http://en.wikipedia.org/wiki/Grayscale
A binary image has only two values for each pixel, 0 and 1 corresponding to black and white (or vice versa). A gray scale image has a certain number (probably 8) bits of information per pixel, hence, 256 possible grey values.
Of course, a grey scale image has a binary representation, but the smallest size of information is not a bit, so we don't call it a binary image.
edit Assuming you want to convert in Matlab, use im2bw. If you're not using Matlab, the idea of binarization is explained on that page as well. It's not difficult to port, it boils down to comparing every pixel to a threshold value.
Black and White image contains only two levels.
Gray image represent by black and white shades or combination of levels for e.g.
8 bit gray image means total 2^8 levels form black to white 0 = black and 255 is White.
Yes,
A binary image is one that consists of pixels that can have one of exactly two colors, usually black and white. It is also called bi-level or two-level.
A gray scale image is a kind of black and white or gray monochrome are composed exclusively of shades of gray.
Yes
Binary Image
A binary image is a black and white image where each pixel value is either 0 or 1. The value 0 represents background or black and the value 1 represents foreground or white.
Grayscale Image
A grayscale image is a black and white image with various shades of gray. The pixel value of a grayscale image is represented as an 8 bit signed integer. i.e values between 0 and 255. The pixel value of the grayscale image represents the brightness of the pixel. The value 0 is black, the value 255 is white, and the values in between makeup different shades of gray.

Resources