我想将滑动窗口(Rect
)移动到每个窗口的一半,但我只能得到第一行:
我的代码:
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错误:cv :: Mat ::中的断言失败(0&lt; = _colRange.start&amp;&amp; _colRange.start&lt; = _colRange.end&amp;&amp; _colRange.end&lt; = m.cols) Mat,文件...... \ opencv \ modules \ core \ src \ matrix.cpp,第292行线程中的异常“AWT-EventQueue-0”CvException [org.opencv.core.CvException:cv :: Exception :. ....... \ opencv \ modules \ core \ src \ matrix.cpp:292:错误:(-215)0&lt; = _colRange.start&amp;&amp; _colRange.start&lt; = _colRange.end&amp;&amp; _colRange.end&lt; = m.cols in function cv :: Mat :: Mat]
我在哪里做错了?
答案 0 :(得分:1)
如果我理解你的问题,你应该纠正你的for循环。
看一下这段代码,看看它是否是预期的结果。代码是用C ++编写的,但是它非常接近Java,我添加了相同的Java调用作为注释(但我没有测试它们)。
#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;
}