简体   繁体   English

MATLAB:从RGB到黑白图像

[英]MATLAB: RGB to white and black image

I have an image which is 160x64x3 . 我有一个160x64x3的图片。 This image has gray points and other colors. 该图像具有灰点和其他颜色。 What I want is to convert the gray parts to 0 and the other parts to 1. 我想要将灰色部分转换为0,将其他部分转换为1。

The image is below: 图片如下:

图片

what I did is below but I think it is not certain because maybe there are some points which are the same for all red, green and blue. 我在下面做了什么,但我认为还不能确定,因为也许有些地方对于所有红色,绿色和蓝色都是相同的。 Is there a special function for this? 为此有特殊功能吗?

~((image(:,:,1)==image(:,:,2))&(image(:,:,1)==image(:,:,3))&(image(:,:,2)==image(:,:,3))) 

I don't know any special function, but the solution is pretty straightforward: 我不知道任何特殊功能,但是解决方案非常简单:

I = imread('sLUp2.png'); %Read source image.

%Initialize all destination pixels to 1
J = ones(size(I,1), size(I,2));

%Set to zero pixels which are gray in I (where Red==Green and Red==Blue).
J((I(:,:,1) == I(:,:,2)) & (I(:,:,1) == I(:,:,3))) = 0;

In the above solution all source gray-scale pixels with R=G=B, are considered gray. 在以上解决方案中,所有具有R = G = B的源灰度像素均被视为灰度。
For example: black pixel: (0,0,0), white pixel (255,255,255) and (x,x,x) are considered gray... 例如:黑色像素(0,0,0),白色像素(255,255,255)和(x,x,x)被视为灰色...


In case you want to find only the single common gray level (not all "gray-scale" pixels), you can do (something) as follows: 如果您只想查找单个普通灰度级(而不是所有“灰度”像素),则可以执行以下操作(某些操作):

R = I(:, :, 1); %Red color plane.
Gray = R((I(:,:,1) == I(:,:,2)) & (I(:,:,1) == I(:,:,3))); %All values in which R=G=B.
H = imhist(Gray, 256); %Collect histogram of Gray.
common_gray = find(H == max(H)) - 1; %Find the most common gray value in histogram.

%Now, set only common_gray pixels to zero in destination image J.
J = ones(size(I,1), size(I,2));
J(R == common_gray) = 0;

In your image the common gray level ("gray points") equals 128 . 在图像中,常见的灰度级(“灰度点”)等于128

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM