开发者

How do I invert a grayscale image and convert it to a binary image?

I want to create an image like this:

How do I invert a grayscale image and convert it to a binary image?

From an image like this: alt text http://internationalpropertiesregistry.com/Server/s开发者_StackOverflowhowFile.php?file=%2FUpload%2Funtitled.jpgb523c7595dd8e7514e1c6d51a83161a3.jpeg

UPDATE

Here's what's generated by 255-img, but the result doesn't match exactly: alt text http://internationalpropertiesregistry.com/Server/showFile.php?file=%2FUpload%2Funtitled.jpg61f9edbeb4f0285c2a1772cced3ce393.jpeg


It depends on what type your input matrix is.

If it is a logical matrix you can simply use

invImg = ~img;

If you have scalar values in the range of 0 to n use

invImg = n - img;

Edit:

If you want a black and white image try the following (perhaps you need to play with the level paramter):

invImg = ~im2bw(img, 0.5);


I had some trouble processing the second image you posted above. Since it's in JPEG format, the image compression seems to have made the lines and text fuzzy, and that complicates thresholding it in the way that you want.

I instead went back to the indexed-color GIF image you posted on the previous related question, converted it to grayscale (using the function IND2GRAY from the Image Processing Toolbox), then converted that to a reversed black and white image to match the format of the first image you posted above. Here's 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's what reversedImage looks like:

How do I invert a grayscale image and convert it to a binary image?


How about just flipping the colormap?

colormap(flipud(colormap));


It looks like the 3rd plot is the reverse of the 2nd above.

One thing to consider is what you are using for draw image image() imagesc() or imshow() imagesc() does auto scaling and imshow() will use a color map.

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


Your images aren't binary black & white. There's gray in there as well.

Given from your edit that you seem to what any on pixel to be off, and any off pixel to be on (ie, convert it to a straight 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;

Note that the 2nd line isn't strictly necessary since the new image is being initialized to 0 anyways, it's just in to show what exactly is going on.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜