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

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

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

5条回答
  •  悲&欢浪女
    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.

提交回复
热议问题