Search code examples
c++opencvimage-processingbackground-subtraction

Creating bigger image out of small image Mat [opencv, background subtraction]


I have an image i1. I am supposed to create another Mat m1 of size (image.rows*3, image.cols*3). In m1, I'm supposed to fill the pixel value in the following way. (Please do see the image):

enter image description here

Here is my code-

#include <highgui.h>  
#include "opencv2/opencv.hpp"  
#include <fstream>  
using namespace cv;
static Mat NeurMap1, NeurMap2, NeurMap3, frame, hsv_Frame;
std::ofstream myfile;
void InitializeNeurMap(cv::Mat Channel[3])
{  
            int i=0,j=0,m_i=0,m_j=0, t1=0, t2=0;

    for(i=0; i < frame.rows; i++)
    {
        for(j=0;j < frame.cols;j++)
        {
            t1= i*n+1; t2 = j*n+1;
            for(m_i=t1-1; m_i <= t1+1;m_i++)
            {
                for(m_j=t2-1; m_j <= t2+1; m_j++)
                {
                    NeurMap1.at<uchar>(m_i,  m_j)= frame.at<uchar>(i,j); 
                }
            }
        }
    }
    std::cout<<m_j;
    myfile<<frame;

}
int main()
{

    myfile.open("NeurMaptext.txt");
    String filename="BootStrap/b%05d.bmp";// sequence of frames are read
    VideoCapture cap(filename);
    if(!cap.isOpened())  // check if we succeeded
        return -1;
    namedWindow("edges",1);
    //namedWindow("frames",1);
    Mat Channel[3];
    cap>>frame;
    NeurMap1 = Mat::zeros(frame.rows*n, frame.cols*n, frame.type());
    InitializeNeurMap(Channel);
    imshow("edges",NeurMap1);waitKey(33);
    for(;;)
    {
        cap>>frame;
        if(frame.empty())
            break;
    }   

    system("pause");
    return 0;
}

The input image is RGB[160*120]. Why am I not getting the columns in the output image given in the link above?.


Solution

  • You can simply call resize() by passing the INTER_NEAREST parameter, i.e. using the nearest-neighbor interpolation.

    #include <opencv2/opencv.hpp>
    #include <iostream>
    using namespace cv;
    using namespace std;
    
    int main()
    {
        unsigned char data[] = { 1, 2, 3, 4, 5, 6 };
        Mat img(2, 3, CV_8UC1, data);
        cout << img << endl;
    
        Mat res(6, 9, CV_8UC1);
        resize(img, res, res.size(), 0, 0, INTER_NEAREST);
        cout << res << endl;
    
        return 0;
    }
    

    You will get:

    enter image description here