1
votes

I want to move a sliding window (a Rect) by half of each window, but I can only get the first line:

My code:

int widthImg = 600;
int HeightImg = 500;
int wWin = 100;// weight window 
int hWin = 100;// height window
int xWin = 0; 
int yWin = 0;
int winSize = ((widthImg/wWin)*2) * ((HeightImg/hWin)*2);// slide half of window(50)

for(int i=0;i<winSize;i++){
    Mat ROIMat = new Mat();               

    if(i < winSize){                 
        xWin = xWin + wWin/2;                    
        if(xWin == widthImg){
             xWin = 0;                           
             yWin = yWin + hWin/2;                                               
         }                   
    }           

     ROIMat = croppMat(Highgui.imread(fileImageName), new Rect(xWin , yWin , wWin , hWin) );                 
     Highgui.imwrite(pathROI+"\\"+i+".jpg", ROIMat); //save ROI image                
}

ERROR:

OpenCV Error: Assertion failed (0 <= _colRange.start && _colRange.start <= _colRange.end && _colRange.end <= m.cols) in cv::Mat::Mat, file......\opencv\modules\core\src\matrix.cpp, line 292 Exception in thread "AWT-EventQueue-0" CvException [org.opencv.core.CvException: cv::Exception: ........\opencv\modules\core\src\matrix.cpp:292: error: (-215) 0 <= _colRange.start && _colRange.start <= _colRange.end && _colRange.end <= m.cols in function cv::Mat::Mat]

Where am I doing wrong?

1
I edited your question to make it more clear. Please check that, and eventually improve the edit. - Miki

1 Answers

1
votes

If I understand correctly your question, you should correct your for loop.

Take a look at this code, and check if it's the expected result. The code is in C++, but it's be very close to Java, and I added as comments the equivalent Java calls (but I didn't test them).

#include <opencv2/opencv.hpp>
#include <string>
using namespace cv;

int main()
{
    // Load image
    Mat3b img = imread(fileImageName);
    // JAVA: Mat img = Highgui.imread(fileImageName);

    int widthImg = img.cols;  // JAVA: img.cols();
    int heightImg = img.rows; // JAVA: img.rows(); 

    int wWin = 100; // weight window 
    int hWin = 100; // height window

    int counter = 0;
    for (int yWin = 0; yWin <= heightImg - hWin; yWin += hWin/2)
    {
        for (int xWin = 0; xWin <= widthImg - wWin; xWin += wWin/2)
        {
            Mat ROIMat(img(Rect(xWin, yWin, wWin, hWin)));
            // JAVA: Mat ROIMat = new Mat(); 
            // JAVA: ROIMat = croppMat(img, new Rect(xWin, yWin, wWin, hWin));

            imwrite(pathROI  + std::to_string(counter) + ".jpg", ROIMat);
            //JAVA: Highgui.imwrite(pathROI + "\\" + counter + ".jpg", ROIMat); //save ROI image  

            ++counter;
        }
    }

    return 0;
}