如何在matlab中访问图像的矩阵并扫描它以找到特定的像素值? [英] How to access image's matrix in matlab and scan it to find the specific pixel values?
问题描述
我需要访问图像的矩阵并从上到下和从左到右扫描它,因为我需要为连接组件标记算法制作代码。但我不知道从哪里开始?因为我不知道如何存储矩阵?因为我是matlab中的新手
循环可以像
i need to access image's matrix and scan it from top to bottom and left to right, because i need to make code for connected components labelling algorithm.But i dont know where to start from? because i dont know how to store matrix? as i am a newbie in matlab Loop can be run like
[rows columns] = size(img);
for a=1:1:rows
for b=1:1:columns
但是,我不确定,这是不是正确的方法?因为我需要访问每个像素值并找到具体值?怎么写,如果pixelvalue = 1,加1吧?
but, i am not sure, if it is the correct way or not? as i need to access each pixel value and find specific value? how to write, if pixelvalue=1, add 1 to it?
推荐答案
在Matlab中你不一定需要使用循环来解析矩阵的值。在这个简单的例子中,您可以使用逻辑索引(请参阅不同的 Matlab中的索引方法)在矩阵中查找特定值并对其进行修改。
In Matlab you do not necessarily need to use loops to parse the values of a matrix. In this simple case you can use logical indexing (see different indexing methods in Matlab) to find specific values in your matrix and modify it.
对于您的特殊情况,您只想修改具有该值的像素1,并将此值设置为2.简单地写:
For your special case you want to modify only pixels that have the value 1 and set this value to 2. It is simply written:
img(img==1)=2;
这篇关于如何在matlab中访问图像的矩阵并扫描它以找到特定的像素值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!