How to convert grayscale image and convert it to binary image?

5 answers

It depends on the type of input matrix.

If it is a logical matrix, you can simply use

 invImg = ~img; 

If you have scalar values ​​between 0 and n, use

 invImg = n - img; 

Edit:

If you want the black and white image to try the following (maybe you need to play with the level parameter):

 invImg = ~im2bw(img, 0.5); 
+5
source

I am having trouble processing the second image you specified above. Since it is in JPEG format, image compression seems to have made lines and text fuzzy, and this makes it difficult to set it in the way you want.

Instead, I went back to the GIF image with the indexed color posted in the previous related question , converted it to grayscale (using the IND2GRAY function from the Image Processing Toolbox ), then converted to the inverse black and white image according to the format of the first image that you specified above. Here is the code I used:

 [X,map] = imread('original_chart.gif'); %# Load the indexed color image img = ind2gray(X,map); %# Convert the image to grayscale reversedImage = img < max(img(:)); %# Convert to reversed black and white 

And here is what reversedImage looks like:

alt text

+3
source

How to just flip a color map?

Colormap (flipud (color map));

+3
source

It appears that the third chart is the inverse of the 2nd above.

You might think that you use image() imagesc() or imshow() imagesc() to scale automatically, and imshow() will use a color map.

Another thing to consider is the input image. Does it range from 0 to 255, 0 to 1.0, or RGB? Depending on what it is, the opposite will be different.

+1
source

Your images are not black and white. There is also a gray color.

Given that you are editing that it seems to you that some pixel is off and any pixel is off (i.e., convert it to a direct binary black and white image), this should do what you want:

 newImg = zeros(size(img)); newImg(img > 0) = 0; % <-- This line is not really needed newImg(img = 0) = 1; 

Please note that the 2nd line is not strictly necessary, since the new image is initialized to 0 in any case, this is just to show what exactly is happening.

+1
source

Source: https://habr.com/ru/post/1343362/


All Articles