gpt4 book ai didi

java - 如何在图像ROI上滑动Rect

转载 作者:太空宇宙 更新时间:2023-11-03 22:54:11 25 4
gpt4 key购买 nike

我想将一个滑动窗口(一个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
}

错误:

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]

我哪里做错了?

最佳答案

如果我理解正确你的问题,你应该更正你的 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;
}

关于java - 如何在图像ROI上滑动Rect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33731696/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com