Scale image in C++ with OpenCV but not using pyrDown() nor pyrUp() functions

Hello I'm trying to scale x2 an image to decrease or increase it, but I am not allowed to call the pyr functions in the code. Here's what I have so far for decreasing the image:

cv::Mat Pyramid::calcPyramid(cv::Mat image)
{
    int channels = image.channels();
    int rows = image.rows;
    int cols = image.cols*image.channels();

    cv::Mat calculada(image.rows/2, image.cols/2, CV_8UC3);
    
    uchar* prixelPtr = (uchar*)image.data;

    for(int iRows = 0; iRows < rows; iRows++)
    {
        for(int iCols=0; iCols < cols; iCols++)
        {
            uchar blue = prixelPtr[ 2*iRows*image.cols*channels + 2*iCols*channels + 0];
            uchar green = prixelPtr[ 2*iRows*image.cols*channels + 2*iCols*channels + 1];
            uchar red = prixelPtr[ 2*iRows*image.cols*channels + 2*iCols*channels + 2];

            calculada.at<cv::Vec3b>(iRows, iCols)[0] = blue;
            calculada.at<cv::Vec3b>(iRows, iCols)[1] = green;
            calculada.at<cv::Vec3b>(iRows, iCols)[2] = red;
        }
    }
    return calculada;
}

The error I'm getting on the terminal is: Segmentation fault (core dumped). How can I fix the code? Thanks!

Upvotes: 0

Views: 344

Answers (1)

Andrey  Smorodov
Andrey Smorodov

Reputation: 10852

The code is simple:

src and dst must be separate images, not the same matrix.

void pyrDown(cv::Mat& src, cv::Mat& dst)
{
    dst = cv::Mat(src.rows / 2, src.cols / 2, CV_8UC3);
    for (int i = 0; i < src.rows/2; ++i)
    {
        for (int j = 0; j < src.cols/2; ++ j)
        {
            // sample 4 points from source image
            cv::Vec3f p11 = src.at<cv::Vec3b>(i * 2, j * 2);
            cv::Vec3f p12 = src.at<cv::Vec3b>(i * 2, j * 2+1);
            cv::Vec3f p21 = src.at<cv::Vec3b>(i * 2+1, j * 2);
            cv::Vec3f p22 = src.at<cv::Vec3b>(i * 2+1, j * 2+1);
            // find mean (we want to keep as much information as possible)
            cv::Vec3f p_dst = (p11 + p12 + p21 + p22) / 4.0f;
            // put result to destination image
            dst.at<cv::Vec3b>(i, j) = p_dst;
        }
    }
}

But it would be great to add boundary checking conditions, hope you'll manage it :)

Upvotes: 1

Related Questions