使用 Mat::at<Vec3i> Point 未构造正确的像素

Use Mat::at<Vec3i> Point not assing the correct pixel

本文关键字:像素 gt at Mat lt Vec3i 使用 Point      更新时间:2023-10-16

我通常会找到通常遇到的问题的所有答案,不幸的是,在这种情况下我找不到解决方案。我有以下代码

Mat drawing = Mat::zeros( threshold_output.size(), CV_8UC3 );
namedWindow( "Contours", CV_WINDOW_AUTOSIZE );
for( int i = 0; i< contours.size(); i++ ){
   Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
   drawContours( drawing, contours_poly, i, color, 1, 8, vector<Vec4i>(), 0, Point() );
   rectangle( drawing, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0 );
    drawing.at<Vec3i>(centroid[i])[0]=color[0];
    drawing.at<Vec3i>(centroid[i])[1]=color[1];
    drawing.at<Vec3i>(centroid[i])[2]=color[2];
    circle(drawing, centroid[i], 5, color, 3,8,0);
    cout<<centroid[i]<<endl;
    imshow( "Contours", drawing );
    waitKey(0);
 }

问题是质心并没有放在应有的位置。这些点的高度相同,但宽度不对。我已经用圆图检查了每个轮廓的"质心",这些点都可以。

有人能在这件事上帮我吗

您试图将3*32位分配给一个只有3*8位大小的字段。您将矩阵定义为CV_8UC3类型,因此应该使用drawing.at<Vec3b>(...)(其中b代表无符号字符)。你可以在这里找到各种类型的def。

您可以将CV_xxtCn读取为

  • xx:位数
  • t: 类型(F=浮点类型,S=有符号整数,U=无符号整数)
  • n: 通道数