【问题标题】:what i must change that i can read the pixel value from the array?我必须改变什么才能从数组中读取像素值?
【发布时间】: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);
        }

【问题讨论】:

    标签: c++ opencv


    【解决方案1】:

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

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

    但是这里,i 的出处,显然是一个 col 的索引。

            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);
    

    【讨论】:

    • 感谢您的帮助,我已经更改了(行和列)的位置,问题是我的代码可以将第一行读到最后,然后当他读完最后一行array(16*16) 他停了下来,没有移动到下一行
    • 我已经更新了我的答案。学习使用调试器可能是个好主意。
    【解决方案2】:

    imread() 必须包含文件的完整路径,并确保使用双破折号,例如

    cv::Mat imageMat = cv::imread(C:\\Folder\\Frame.jpg", CV_LOAD_IMAGE_COLOR);
    

    不必指定grayscaleMatbinaryMat 的维度或类型,因为opencv 函数会为您准备好它们。

    在您的 cvtColor 函数中,它应该是 CV_RGB2GRAY 而不是 CV_BGR2GRAY。

    在你的阈值函数中,它不应该是 cv::THRESH_BINARY 而是 CV_THRESH_BINARY

    希望这会有所帮助。

    【讨论】:

    • 异常肯定在at()中,不需要提供文件的完整路径,只要你提供的任何位置都是有效的。
    • @B...,opencv默认看哪个位置?
    • 与其说是 opencv 的外观,不如说是 C stio 库的外观。您提供的文件名最终会传递给 fopen()。因此,当前工作目录或文件夹中没有路径。
    • 感谢您的解释。就我而言,我通常将视频保存在桌面上的单独文件夹中,因此添加了路径。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-14
    • 2019-09-27
    • 1970-01-01
    • 1970-01-01
    • 2014-07-13
    • 1970-01-01
    相关资源
    最近更新 更多