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

前端 未结 5 1335
栀梦
栀梦 2021-01-14 07:40

I want to create an image like this: \"alt From an image like this:

相关标签:
5条回答
  • 2021-01-14 07:52

    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:

    alt text

    0 讨论(0)
  • 2021-01-14 08:04

    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);
    
    0 讨论(0)
  • 2021-01-14 08:04

    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.

    0 讨论(0)
  • 2021-01-14 08:04

    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 讨论(0)
  • 2021-01-14 08:06

    How about just flipping the colormap?

    colormap(flipud(colormap));

    0 讨论(0)
提交回复
热议问题