【发布时间】:2013-11-18 13:55:16
【问题描述】:
我有以下代码。我从图像中读取了一个像素块,我想从每个块(数组 16*16)中获取值。 但是,我收到此错误:
OpenCV 错误:断言失败 (dims ::channels)
((DataType<_tp>::depth) & ((1
我应该改变什么才能运行我的代码?
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);
}
【问题讨论】: