【OpenCV】Vec3b类型的含义与使用 vector<uchar, 3>

定义
Vec3b可以看作是vector<uchar, 3>。

简单而言就是一个uchar类型的,长度为3的vector向量。

使用
由于在OpenCV中,使用imread读取到的Mat图像数据,都是用uchar类型的数据存储,对于RGB三通道的图像,每个点的数据都是一个Vec3b类型的数据。使用at定位方法如下:

img.at<Vec3b>(row, col)[0] = 255;  // 这是指修改B通道数据
img.at<Vec3b>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
将下列代码转换成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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

R-G-B

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值