通过 rgb 值 matlab 获得像素坐标
get pixel coordination by rgb values matlab
如何在 matlab 中通过 rgb 值获取图像中像素的 x,y 坐标?
例如:我有一张图像,我想找到其中黑色区域的像素坐标..
有一个内置函数可以执行此操作:impixel
。
来自官方文档:
Return Individual Pixel Values from Image
% read a truecolor image into the workspace
RGB = imread('myimg.png');
% determine the column c and row r indices of the pixels to extract
c = [1 12 146 410];
r = [1 104 156 129];
% return the data at the selected pixel locations
pixels = impixel(RGB,c,r)
% result
pixels =
62 29 64
62 34 63
166 54 60
59 28 47
Link: https://it.mathworks.com/help/images/ref/impixel.html
[编辑]
好的,我误解了你的问题。因此,要完成您要查找的内容,只需使用以下代码:
img = imread('myimg.png');
r = img(:,:,1) == uint8(0);
g = img(:,:,2) == uint8(0);
b = img(:,:,3) == uint8(255);
[rows_idx,cols_idx] = find(r & g & b);
上面的示例找到图像 (#0000FF) 内的所有纯蓝色像素和 returns 它们的索引。您还可以避免将值强制转换为 uint8
,它应该可以通过在比较期间隐式转换值来工作。
如果要查找值为 (R, G, B)
的像素的所有坐标,则
[y, x] = find(img(:,:,1)==R & img(:,:,2)==G & img(:,:,3)==B);
对于黑色像素选择 R=0, G=0, B=0
如何在 matlab 中通过 rgb 值获取图像中像素的 x,y 坐标?
例如:我有一张图像,我想找到其中黑色区域的像素坐标..
有一个内置函数可以执行此操作:impixel
。
来自官方文档:
Return Individual Pixel Values from Image
% read a truecolor image into the workspace
RGB = imread('myimg.png');
% determine the column c and row r indices of the pixels to extract
c = [1 12 146 410];
r = [1 104 156 129];
% return the data at the selected pixel locations
pixels = impixel(RGB,c,r)
% result
pixels =
62 29 64
62 34 63
166 54 60
59 28 47
Link: https://it.mathworks.com/help/images/ref/impixel.html
[编辑]
好的,我误解了你的问题。因此,要完成您要查找的内容,只需使用以下代码:
img = imread('myimg.png');
r = img(:,:,1) == uint8(0);
g = img(:,:,2) == uint8(0);
b = img(:,:,3) == uint8(255);
[rows_idx,cols_idx] = find(r & g & b);
上面的示例找到图像 (#0000FF) 内的所有纯蓝色像素和 returns 它们的索引。您还可以避免将值强制转换为 uint8
,它应该可以通过在比较期间隐式转换值来工作。
如果要查找值为 (R, G, B)
的像素的所有坐标,则
[y, x] = find(img(:,:,1)==R & img(:,:,2)==G & img(:,:,3)==B);
对于黑色像素选择 R=0, G=0, B=0