我对Matlab很新,在使用图像时遇到了问题。 我想在下图中得到一个特定颜色(蓝色)的像素: image 我当前的代码看起来像这样:
function p = mark(image)
%// display image I in figure
imshow(image);
%// first detect all blue values higher 60
high_blue = find(image(:,:,3)>60);
%cross elements is needed as an array later on, have to initialize it with 0
cross_elements = 0;
%// in this iteration the marked values are reduced to the ones
%where the statement R+G < B+70 applies
for i = 1:length(high_blue)
%// my image has the size 1024*768, so to access the red/green/blue values
%// i have to call the i-th, i+1024*768-th or i+1024*768*2-th position of the "array"
if ((image(high_blue(i))+image(high_blue(i)+768*1024))<...
image(high_blue(i)+2*768*1024)+70)
%add it to the array
cross_elements(end+1) = high_blue(i);
end
end
%// delete the zero element, it was only needed as a filler
cross_elements = cross_elements(cross_elements~=0);
high_vector = zeros(length(cross_elements),2);
for i = 1:length(cross_elements)
high_vector(i,1) = ceil(cross_elements(i)/768);
high_vector(i,2) = mod(cross_elements(i), 768);
end
black = zeros(768 ,1024);
for i = 1:length(high_vector)
black(high_vector(i,2), high_vector(i,1)) = 1;
end
cc = bwconncomp(black);
a = regionprops(cc, 'Centroid');
p = cat(1, a.Centroid);
%// considering the detection of the crosses:
%// RGB with B>100, R+G < 100 for B<150
%// consider detection in HSV?
%// close the figure
%// find(I(:,:,3)>150)
close;
end
但显然没有针对Matlab进行优化。 所以我想知道是否有办法搜索具有特定值的像素, 其中蓝色值大于60(使用find命令不难, 但与此同时,红色和绿色区域的值不会太高。 有一个我失踪的命令吗? 由于英语不是我的母语,如果你给我一些合适的谷歌搜索关键词,它甚至可能有帮助;) 提前致谢
答案 0 :(得分:1)
根据您在代码末尾的问题,您可以在一行中获得所需内容:
NewImage = OldImage(:,:,1) < SomeValue & OldImage(:,:,2) < SomeValue & OldImage(:,:,3) > 60;
imshow(NewImage);
例如,如您所见,您使用逻辑运算符为每个通道提供限制,您可以自定义(例如,使用|作为逻辑OR)。这是你想要的?根据你的代码,你似乎在寻找图像中的特定区域,如十字架或硬币就是这种情况?如果我给你的代码完全偏离轨道,请提供更多细节:)
简单示例:
A = imread('peppers.png');
B = A(:,:,3)>60 & A(:,:,2)<150 & A(:,:,1) < 100;
figure;
subplot(1,2,1);
imshow(A);
subplot(1,2,2)
imshow(B);
给予: