对于img.at<uchar>(j, i) 和img.at<uchar>(Point(j, i)) 的区别

我说说我的理解:

srcImage.at<uchar>(j, i) //表示的是  j 行 i 列 的这个像素
srcImage.at<uchar>(Point(j, i)) //表示的是 坐标(j,i)的像素
1
2
为了我下面的图方便说明 j 换成 x , i 换成 y 。也就是说:

srcImage.at<uchar>(x, y) //表示的是 x 行 y 列 的这个像素
srcImage.at<uchar>(Point(x, y)) //表示的是 坐标(x,y)的像素
1
2
可能就会有人不明白了,有什么区别呢?? 
先上一幅图 
 坐标图
问图中红点的坐标是? (2,1) 还是 (1,2)? 
答案是(2,1)! 
为什么? 看清了 我问的是坐标!! 
那么(2,1) 和 (1,2)究竟有什么不同? 
(2,1)表示的是红点的坐标,就是红点的 x 坐标是 2, y 坐标是 1。 
(1,2)表示的是红点的行和列,就是红点的行是1,列是2,也就是红点在第 1 行,第 2 列。 
这下子我相信大家都明白这两者的区别了吧

srcImage.at<uchar>(j, i)
srcImage.at<uchar>(Point(j, i))
 

将下列代码转换成python代码 #include <opencv2/opencv.hpp> #include <vector> #include <time.h> using namespace cv; using namespace std; // 8邻域 const Point neighbors[8] = { { 0, 1 }, { 1, 1 }, { 1, 0 }, { 1, -1 }, { 0, -1 }, { -1, -1 }, { -1, 0 }, {-1, 1} }; int main() { // 生成随机数 RNG rng(time(0)); Mat src = imread("1.jpg"); Mat gray; cvtColor(src, gray, CV_BGR2GRAY); Mat edges; Canny(gray, edges, 30, 100); vector<Point> seeds; vector<Point> contour; vector<vector<Point>> contours; int i, j, k; for (i = 0; i < edges.rows; i++) for (j = 0; j < edges.cols; j++) { Point c_pt = Point(i, j); //如果当前点为轮廓点 if (edges.at<uchar>(c_pt.x, c_pt.y) == 255) { contour.clear(); // 当前点清零 edges.at<uchar>(c_pt.x, c_pt.y) = 0; // 存入种子点及轮廓 seeds.push_back(c_pt); contour.push_back(c_pt); // 区域生长 while (seeds.size() > 0) { // 遍历8邻域 for (k = 0; k < 8; k++) { // 更新当前点坐标 c_pt.x = seeds[0].x + neighbors[k].x; c_pt.y = seeds[0].y + neighbors[k].y; // 边界界定 if ((c_pt.x >= 0) && (c_pt.x <= edges.rows - 1) && (c_pt.y >= 0) && (c_pt.y <= edges.cols - 1)) { if (edges.at<uchar>(c_pt.x, c_pt.y) == 255) { // 当前点清零 edges.at<uchar>(c_pt.x, c_pt.y) = 0; // 存入种子点及轮廓 seeds.push_back(c_pt); contour.push_back(c_pt); }// end if } } // end for // 删除第一个元素 seeds.erase(seeds.begin()); }// end while contours.push_back(contour); }// end if } // 显示一下 Mat trace_edge = Mat::zeros(edges.rows, edges.cols, CV_8UC1); Mat trace_edge_color; cvtColor(trace_edge, trace_edge_color, CV_GRAY2BGR); for (i = 0; i < contours.size(); i++) { Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)); //cout << edges[i].size() << endl; // 过滤掉较小的边缘 if (contours[i].size() > 5) { for (j = 0; j < contours[i].size(); j++) { trace_edge_color.at<Vec3b>(contours[i][j].x, contours[i][j].y)[0] = color[0]; trace_edge_color.at<Vec3b>(contours[i][j].x, contours[i][j].y)[1] = color[1]; trace_edge_color.at<Vec3b>(contours[i][j].x, contours[i][j].y)[2] = color[2]; } } } imshow("edge", trace_edge_color); waitKey(); return 0; }
05-25
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值