gpt4 book ai didi

c++ - 我必须改变什么才能从数组中读取像素值?

转载 作者:太空宇宙 更新时间:2023-11-03 23:00:14 25 4
gpt4 key购买 nike

我有以下代码。我从图像中读取了一个像素 block ,我想从每个 block (数组 16*16)中获取值。但是,我收到此错误:

OpenCV Error: Assertion failed (dims <= 2 && data && (unsigned)i0 < (unsigned)size.p[0] && (unsigned)(i1*DataType<_Tp>::channels) < (unsigned)(size.p[1]*channels()) && ((((sizeof(size_t)<<28)|0x8442211)

((DataType<_Tp>::depth) & ((1 << 3) - 1))*4) & 15) == elemSize1()) in unknown function, file C:\opencv231\build\include\opencv2/core/mat.hpp, line 537

我应该更改什么才能运行我的代码?

enum Color {White, Black}; 
Color checkBlock(Mat& img, int& i, int& j, double& T)
{
unsigned int Sum=0;
for(int k=0;k<16;k++)
for(int l=0;l<16;l++)
Sum += img.at<unsigned char>(i+k,j+l);
double Average = Sum/256;
std::cout << Average << std::endl;
return (Average > T) ? (White) : (Black);
}

void main()
{
Mat img = imread("Frame.jpg",0);
namedWindow( "Display window", CV_NORMAL );// Create a window for display.
if(!img.data)
std::cout << "error";
// STEPS TO CONVERT TO BINARY IMAGE
// LOAD THE IMAGE
cv::Mat imageMat = cv::imread("Frame.jpg", CV_LOAD_IMAGE_COLOR);

cv::Mat grayscaleMat (imageMat.size(), CV_8U);

//Convert BGR to Gray
cv::cvtColor(imageMat, grayscaleMat, CV_BGR2GRAY );

//Binary image
cv::Mat binaryMat(grayscaleMat.size(), grayscaleMat.type());

//Apply thresholding
cv::threshold(grayscaleMat, binaryMat, 100, 255, cv::THRESH_BINARY);

//Show the results
// cv::namedWindow("Output",CV_NORMAL);
//cv::imshow("Output", binaryMat);

// cv::waitKey(0);

double minVal, maxVal;
minMaxLoc(img,&minVal,&maxVal,NULL,NULL);
double Threshold = 0.5 * (minVal + maxVal);
int i=4,j=4;
Size s = img.size();
Color old_c, new_c;
// define the position wher i will begin to read the first row from the image
for (j=16*55;j<=s.height;j=j+16)
for(i=0;i<=s.width;i=i+16)
{
Point x(i,j);
Point y(i+16,j+16);
//std::cout << x << " " << y << std::endl;
rectangle(img, x, y, Scalar(255,0,0),3);
Color c = checkBlock(img,i,j,Threshold);
}

最佳答案

在这一行中,您使用 i 来索引一行:

        Sum += img.at<unsigned char>(i+k,j+l);

但是这里,i的来源,明明是一个col的index。

        for(i=0;i<=s.width;i=i+16)

所以第一行应该是:

Sum += img.at<unsigned char>(j+l, i+k);

要清楚一点,at 的参数是 (row, col),Point 的参数是 (x,y),这有点陷阱。

还有

for (j=16*55;j<=s.height;j=j+16)
for(i=0;i<=s.width;i=i+16)
...
Point y(i+16,j+16);

应该是

for (j = 16 *  55; j < s.height - 15 ; j = j + 16)
for(i = 0; i < s.width - 15; i = i + 16)
...
Point y(i + 15, j + 15);

关于c++ - 我必须改变什么才能从数组中读取像素值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20049545/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com