OpenCV:如何迭代图像特定区域中的每个像素 [英] OpenCV: How to iterate each pixel in a specific area of an image
本文介绍了OpenCV:如何迭代图像特定区域中的每个像素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个用两条平行的绿线标记的图像(见下文).在C ++中的OpenCV Mat中读取此图像,绿线的斜率及其到图像中心的距离是已知的.
I have an Image(see below) that is marked by two parallel green lines. This image is read in a OpenCV Mat in C++, the slope of green lines and their distances to the image center are already known.
现在,我要迭代这两条绿线之间区域中的所有像素.我怎么解决这个问题?如果有人可以给我一个代码示例,这将非常有帮助.
Now I want to iterate all the pixels in the area between these two green lines. How can i solve this problem? It will be very helpful, if somebody can give me a code example.
非常感谢.
推荐答案
您可以使用 LineIterator
看看下面的示例代码
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
using namespace std;
using namespace cv;
int main( int, char** argv )
{
Mat src;
src = imread( argv[1] );
if( src.empty() )
{
return -1;
}
Point pt1 = Point( (src.cols / 5) + (src.cols / 8), src.rows);
Point pt2 = Point( (src.cols ) - (src.cols / 8), 0);
LineIterator it(src, pt1, pt2, 8);
for(int y = 0; y < it.count; y++, ++it)
{
Point it_pos = it.pos();
for(int x = it_pos.x; x < it_pos.x+(src.cols / 5) & x < src.cols; x++)
{
Vec3b & pixel = src.at<Vec3b>(it_pos.y,x);
pixel = pixel * 1.3;
pixel[0] = 0;
}
}
imshow("result", src );
waitKey(0);
return 0;
}
结果图像(查看编辑历史记录以更好地了解更改):
result images (look edit history to understand changes well):
这篇关于OpenCV:如何迭代图像特定区域中的每个像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文