Red Viper
Red Viper

Reputation: 507

copying pixel by pixel in openCV

I have a code where i will copy a video to another video When i copy it somehow the angle change

heres a link for a picture

http://i24.photobucket.com/albums/c22/Klifford_Kho/wrongpixel_zpsloshtqqy.png

 Mat frame;
Mat processedImage;





void copy()
{


for (int i = 0; i<400; i++)
{
    for (int j = 0; j<200; j++)
    {

        int b = frame.at<cv::Vec3b>(i, j)[0];
        int g = frame.at<cv::Vec3b>(i, j)[1];
        int r = frame.at<cv::Vec3b>(i, j)[2];


        processedImage.at<cv::Vec3b>(i, j)[0] = b;
        processedImage.at<cv::Vec3b>(i, j)[1] = g;
        processedImage.at<cv::Vec3b>(i, j)[2] = r;


    }
}



int main()
{
VideoCapture cap(0); // get first cam
while (cap.isOpened())
{

    if (!cap.read(frame)) // cam might need some warmup
        continue;
    processedImage = cv::Mat(frame.size().height, frame.size().width, CV_8UC1);
    processedImage.setTo(cv::Scalar::all(0));

    copy();




    imshow("Original", frame);
    imshow("Processed", processedImage);
    if (waitKey(10) == 27)
        break;
}
return 0;

}

P.S. I didnt use frame.cols and frame.rows in the condition because it generated an error heres a picture of the error http://i24.photobucket.com/albums/c22/Klifford_Kho/wrongpixel_zpswze5qjrr.png

Upvotes: 1

Views: 1020

Answers (1)

Andrey  Smorodov
Andrey Smorodov

Reputation: 10852

It is because you create single channel destination image.

processedImage = cv::Mat(frame.size().height, frame.size().width, CV_8UC1);

Change CV_8UC1 to CV_8UC3, it should help also with error mentioned at question end.

Upvotes: 2

Related Questions