通过 RGB 值 MATLAB 获取像素协调



如何通过 matlab 中的 RGB 值获取图像中像素的 x,y 坐标?

例如:我有一张图像,我想在其中定位黑色区域的像素坐标。

如果要

查找值(R, G, B)的像素的所有坐标,则

[y, x] = find(img(:,:,1)==R & img(:,:,2)==G & img(:,:,3)==B);

对于黑色像素,请选择R=0, G=0, B=0

有一个内置函数可以做到这一点: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

链接: 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( 内的所有纯蓝色像素并返回它们的索引。您还可以避免将值强制转换为 uint8 ,无论如何,它都应该通过在比较期间隐式转换值来工作。

最新更新