1
votes

I am writing a simple c++ application using visual studio and opencv that stores Red, Green and Blue values from an image(named src in the code) and stores each Red, Green, Blue pixel values individually in 3 different Mat objects(named RM,BM,GM in the code). I saw this stackOverflow question and did exactly as the first answer explained. I was able to save all the pixel values just fine, but wasn't able to change pixel values of other images because an Abort() has been called. This is the console window after I run the code. Console Window

#include<opencv2/core.hpp>
#include<opencv2/highgui.hpp>
#include<opencv2/imgproc.hpp>
#include <stdio.h>
using namespace cv;
using namespace std;
int main() {
    String file_name = "C:\\images\\haaand.jpg";
    Mat src;
    Mat RM, BM, GM;
    //RM.create(src.cols, src.rows, CV_8UC(2));
    //BM.create(src.cols, src.rows, CV_8UC(2));
    //GM.create(src.cols, src.rows, CV_8UC(2));
    Vec3b intensity;
    Vec3b To[3];
    src = imread(file_name);
    imshow("src", src);
    printf("cols:%d rows:%d \n", src.cols, src.rows);
    for (int i = 0; i < src.cols; i++) {
        for (int j = 0; j < src.rows; j++) {
            intensity = src.at<Vec3b>(j, i);
            printf("intensity:%d %d %d \n", intensity[0], intensity[1], intensity[2]);
            for (int k = 0; k < 3; k++) {
                //uchar bla;
                //bla = intensity[k];
                for (int p = 0; p < 3; p++) {
                    To[k][p] = intensity[k];
                }
                printf("(k:%d) %d %d %d\n", k, To[k][0], To[k][1], To[k][2]);
            }
            printf("all done\n");
            BM.at<Vec3b>(j, i) = To[0];
            GM.at<Vec3b>(j, i) = To[1];
            RM.at<Vec3b>(j, i) = To[2];
        }
    }
    imshow("RM", RM);
    imshow("BM", BM);
    imshow("GM", GM);
    return 0;
}

Could anyone tell me why why this error might happen?

1

1 Answers

0
votes
//RM.create(src.cols, src.rows, CV_8UC(2));
//BM.create(src.cols, src.rows, CV_8UC(2));
//GM.create(src.cols, src.rows, CV_8UC(2));
...
BM.at<Vec3b>(j, i) = To[0];

RM, BM, and GM are not setup. The debugger should show an error when you try to set BM.at<Vec3b>(j, i).

Try instead:

int main() 
{
    String file_name = "C:\\images\\haaand.jpg";
    Mat src = imread(file_name);
    Mat RM = Mat(src.size(), CV_8UC3);
    Mat BM = Mat(src.size(), CV_8UC3);
    Mat GM = Mat(src.size(), CV_8UC3);
    Vec3b intensity;
    Vec3b To[3];
    for(int i = 0; i < src.cols; i++) 
    {
        for(int j = 0; j < src.rows; j++) 
        {
            intensity = src.at<Vec3b>(j, i);
            for(int k = 0; k < 3; k++) 
                for(int p = 0; p < 3; p++) 
                    To[k][p] = intensity[k];
            BM.at<Vec3b>(j, i) = To[0];
            GM.at<Vec3b>(j, i) = To[1];
            RM.at<Vec3b>(j, i) = To[2];
        }
    }

    imshow("src", src);
    imshow("RM", RM);
    imshow("BM", BM);
    imshow("GM", GM);
    waitKey(0);
    return 0;
}