找到第一个红色(255,0,0)像素位置?使用matlab [英] finding 1st red (255,0,0) pixel position? using matlab

查看:386
本文介绍了找到第一个红色(255,0,0)像素位置?使用matlab的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Matlab查找图像中第一个红色像素的位置.

I trying to find first red colour's pixel position in the image using matlab.

我做什么

I = imread('test.jpg');

row = size(I,1);
col = size(I,2);

for rr = 1:row
    for cc = 1:col
        if [rr cc :] = [ 255 0 0 ]
            flag =1;
            break;
        end
     end
end

我的代码有什么问题?

推荐答案

您的if语句有问题.
首先,=运算符和==运算符之间存在根本区别(不仅在matlab中).第一个(=)是**赋值运算符,第二个(==)是布尔测试-这就是您想要的.
其次,您需要比较位置rr,cc处的图像值,而不是位置本身.

Your if statement is problematic.
First, there is a fundamental difference between = operator and == operator (not only in matlab). While the first (=) is an **assignment operator, the second (==) is a boolean test - which is what you want.
Second, you need to compare the image values at location rr,cc and not the locations themselves.

if squeeze( I(rr,cc,:) ) == [255;0;0]

更有可能按照您的期望去做.

Is more likely to do what you expect.

尽管我们正在做,但是向量化matlab语句是一种更好的做法.像这样:

While we are at it, it is a better practice to vectorize matlab statements. Something like:

[rr cc] = find( I(:,:,1) == 255 & I(:,:,2) == 0 & I(:,:,3) == 0 , 1, 'first' );

编辑:有关矢量化方法的更多详细信息:
表达式

Some more details on the vectorized method:
The expression

 I(:,:,1) == 255 & I(:,:,2) == 0 & I(:,:,3) == 3

创建一个二维布尔数组,其大小与I(除第三维)相同,每个红色像素均带有true,否则为false.
find 返回布尔表达式.

Creates a 2D boolean matrix, the same size as I (apart from the third dimension) with true for each red pixel and false otherwise.
The find returns the indices of the true entries of the boolean expression.

这篇关于找到第一个红色(255,0,0)像素位置?使用matlab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆