假设我有这张图片:
我需要用rgb =(0,80,41)来获得最小像素。找到我会做的像素:
bart = imread('bart.jpg');
% Find pixels that have red=0, g = 80 or b = 41
r_find = find(bart(:, :, 1) == 0);
g_find = find(bart(:, :, 2) == 80);
b_find = find(bart(:, :, 3) == 41);
% intersect pixels to find pixels that his color are (0,80,41)
find_res = intersect(intersect(r_find, g_find), b_find);
%find the minimum pixel (1D)
pixel = min(find_res);
是的,我已经创建了它,但是如何获得像素(2D)的x,y坐标?
答案 0 :(得分:1)
您的错误是分别对每个颜色通道使用find
操作。
首先应用条件的解决方案很简单:
[row, col] = find(((bart(:, :, 1) == 0) & (bart(:, :, 2) == 80) & (bart(:, :, 3) == 41)), 1)
上面的示例,首先最小化行坐标。
如果您需要最小化列拳,可以在应用find
之前进行转置:
[col, row] = find([((bart(:, :, 1) == 0) & (bart(:, :, 2) == 80) & (bart(:, :, 3) == 41))]', 1)
通过例子说明:
%Read input image
RGB = imread('https://i.stack.imgur.com/2sRiY.jpg');
%Unpack RGB planes (just for example purpose - you don't need to do it).
R = RGB(:, :, 1);
G = RGB(:, :, 2);
B = RGB(:, :, 3);
%(R == 0) is a logical matrix with 1 where condition is true, and 0 where false.
%Same think for (G == 80) and for (B == 41)
figure;imshow(R == 0); %Same as imshow(RGB(:,:,1) == 0)
figure;imshow(G == 80);
figure;imshow(B == 41);
图片:
R == 0
%Now use AND operation between the three logical matrices:
A = ((RGB(:, :, 1) == 0) & (RGB(:, :, 2) == 80) & (RGB(:, :, 3) == 41));
%A is a logical matrix with 1 where condition is true, and 0 where false.
figure;imshow(A);
%The following operation minimize column first:
%Find first index where A equals true (where value equals 1).
[col, row] = find(A, 1);
%In case you need to minimize the row first, you can transpose A:
[row, col] = find(A', 1);
%All operations in single statement:
[row, col] = find(((RGB(:, :, 1) == 0) & (RGB(:, :, 2) == 80) & (RGB(:, :, 3) == 41)), 1);
答案 1 :(得分:0)
您可以绘制图像的对角线(使用poly2mask
函数)并找到沿对角线指定值的第一个像素。
[rs ,cs,~] = size(bart);
%create a mask image that has diagonal pixels value of 1
mask = poly2mask([0 cs cs], [0 rs rs],rs, cs);
%setting last argument to 1 to find the first pixel that meets the condition
[r c] = find(mask & bart(:, :, 1) == 0 & bart(:, :, 2) == 80 & bart(:, :, 3) == 41,1)
更有效的方法是使用两个角点找到对角线的方程,并且仅使用沿线的像素。