将未处于单一颜色的特定强度的像素清零

时间:2015-03-05 16:34:13

标签: image matlab opencv processing

我在用正确的术语围绕我的问题时遇到一点困难,所以我只是试着用它来刺激它,也许我可以帮助澄清它解决方案。

我想检测图像中的一些彩色灯光,所以我需要一种方法:

a)确定像素的颜色

b)确定它们的“强烈”或“明亮”

c)使用上面的两个值作为是否丢弃给定像素的阈值或标准

我认为单独使用亮度可能不是一个很好的方法,因为环境光会非零。

谢谢!

编辑:因此,使用MATLAB的颜色阈值处理器,我能够通过限制HSV空间中的色调范围来隔离彩色灯光。只是想通过命令行找出一种方法。

1 个答案:

答案 0 :(得分:1)

有两个单独的步骤。 1是找出你想要分离的东西,2是隔离

1)好像你弄明白这一点。但是对于未来,您可以使用" imtool"命令。它与imshow几乎相同,但它允许您检查像素值(RGB,您可以使用rgb2hsv将这些值转换为HSV),裁剪图像,缩放,测量距离等。这可能非常有用。

imtool(my_im)

会打开窗户,非常简单。

2)现在您已经拥有了自己的价值观。您正在寻找的术语是 MASKING misk通常是二进制矩阵/向量,其中1(s)(true)对应于感兴趣的区域而0(s)(false)对应于其他地方。 Matlab将这些称为逻辑"阵列。所以,我们只是说你发现你感兴趣的领域如下

hue=0.2 to 0.3, saturation=don't care, brightness= greater than .5

你可以通过对像素进行二进制比较来创建掩码。我会把它分成三个步骤,这样你就可以理解一切。

%% MASKING STEPS
hue_idx = 1; sat_idx =2 ; bright_idx = 3;
hue_mask = ((my_hsv_im(:,:,hue_idx ) > 0.2) & (my_hsv_im(:,:,hue_idx ) < 0.3));
%note we have no saturation mask, because it would be filled with ones
%since we dont care about the saturation values
brightness_mask = (my_hsv_im(:,:,bright_idx ) > 0.5);
total_mask = hue_mask & brightness_mask; 

%% ALL THE REST
%now we mask your image, recall that 1's are ares of interest and 0's are 
%nothing so just multiply your image by your mask
% the mask is a logical array size MxNx1, we need to convert it to the same
%type as our image in order to multiply them
mask_3d(:,:,hue_idx) = total_mask;
mask_3d(:,:,sat_idx) = total_mask;
mask_3d(:,:,bright_idx) = total_mask;
mask_3d = uint8(mask_3d);    %this step is pretty important, if your image 
                             %is a double use double(mask_3d) instead
masked_rgb_im = my_im .* mask_3d;

%does some plotting just for fun
figure(10);
subplot(2,3,1);imshow(my_im);title('original image');
subplot(2,3,2);imshow(hue_mask);title('hue mask');
subplot(2,3,3);imshow(brightness_mask);title('bright mask');
subplot(2,3,4);imshow(total_mask);title('total mask');
subplot(2,3,5:6);imshow(masked_rgb_im );title('masked image');