cvSetImageROI 不工作
cvSetImageROI not working
我遇到 cvSetImageROI 无法正常工作的问题。请帮我解决这个问题。
我有一个图像(例如,original_image 用于约定),我试图将其分成三个相等的部分。为方便起见,我将生成的三个矩形命名为 rectangle1、rectangle2、rectangle3。由于我的图像是矩形的形式,所以每个生成的矩形都有 original_image 宽度的 1/3。
我对这张图片的想法是,将图片的宽度除以 3,所以结果将等于三个矩形的宽度。
虽然当我尝试这个时,我的 setImageROI 不工作。
我在这里发布提取矩形 1 的代码片段,请指导我哪里出错了。她的x和y值是original_image的矩形的值。
int resultant_rectangle_width = ((int)(original_image->width/3));
int resultant_rectangle_height = (int)original_image->height;
cvSetImageROI(original_image, cvRect(x, y,resultant_rectangle_width,resultant_rectangle_height));
//this cout block was to check if the ROI was set on original_image,but it showed the original_image properties here.
cout<<"original_image width after setting roi:"<<original_image->width<<endl;
cout<<"original_image height after setting roi:"<<original_image->height<<endl;
//copying the ROI to another image
IplImage *rectangle1 = cvCreateImage(cvGetSize(original_image),original_image->depth,original_image->nChannels);
cvCopy(original_image, rectangle1);//using three arguments also did not help
cvResetImageROI(original_image);
cvShowImage("rectangle1", rectangle1);
我试图显示的矩形 1 显示错误的裁剪图像,即高度错误。我不明白我在哪里更改 original_image 的高度,因为 rectangle1 显示错误的高度。
我期待代码给我一个与原始图像具有相同高度的图像,但其宽度是原始图像的 1/3。
试过你的代码,但我没有发现任何错误。也许您的 x
和 y
在代码段中不为零。
添加cvWaitKey(0)
只是为了调试,对您的代码没有影响。
您的代码:
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/nonfree/features2d.hpp>
#include <opencv2/nonfree/nonfree.hpp>
#include <opencv2/opencv.hpp>
using namespace std;
int main(){
IplImage *original_image = cvLoadImage("C:/1.jpg", 0);
cvShowImage("orig", original_image);
int x = 0;
int y = 0;
int resultant_rectangle_width = ((int)(original_image->width/3));
int resultant_rectangle_height = (int)original_image->height;
//Remember that (x,y) is the coordination of the up-left corner.
//Official Document: http://docs.opencv.org/2.4.9/modules/core/doc/basic_structures.html
cvSetImageROI(original_image, cvRect(x, y,resultant_rectangle_width,resultant_rectangle_height));
//this cout block was to check if the ROI was set on original_image,but it showed the original_image properties here.
cout<<"original_image width after setting roi:"<<original_image->width<<endl;
cout<<"original_image height after setting roi:"<<original_image->height<<endl;
cvShowImage("3", original_image);
cvWaitKey(0);
//copying the ROI to another image
IplImage *rectangle1 = cvCreateImage(cvGetSize(original_image),original_image->depth,original_image->nChannels);
cvCopy(original_image, rectangle1);//using three arguments also did not help
cvResetImageROI(original_image);
cvShowImage("rectangle1", rectangle1);
cvWaitKey(0);
return 0;
}
结果:
整张图片
投资回报率
如你所见,他们确实一样高。因此,您很有可能错误地将 x
和 y
设置为某个错误的值。
我遇到 cvSetImageROI 无法正常工作的问题。请帮我解决这个问题。
我有一个图像(例如,original_image 用于约定),我试图将其分成三个相等的部分。为方便起见,我将生成的三个矩形命名为 rectangle1、rectangle2、rectangle3。由于我的图像是矩形的形式,所以每个生成的矩形都有 original_image 宽度的 1/3。
我对这张图片的想法是,将图片的宽度除以 3,所以结果将等于三个矩形的宽度。
虽然当我尝试这个时,我的 setImageROI 不工作。
我在这里发布提取矩形 1 的代码片段,请指导我哪里出错了。她的x和y值是original_image的矩形的值。
int resultant_rectangle_width = ((int)(original_image->width/3));
int resultant_rectangle_height = (int)original_image->height;
cvSetImageROI(original_image, cvRect(x, y,resultant_rectangle_width,resultant_rectangle_height));
//this cout block was to check if the ROI was set on original_image,but it showed the original_image properties here.
cout<<"original_image width after setting roi:"<<original_image->width<<endl;
cout<<"original_image height after setting roi:"<<original_image->height<<endl;
//copying the ROI to another image
IplImage *rectangle1 = cvCreateImage(cvGetSize(original_image),original_image->depth,original_image->nChannels);
cvCopy(original_image, rectangle1);//using three arguments also did not help
cvResetImageROI(original_image);
cvShowImage("rectangle1", rectangle1);
我试图显示的矩形 1 显示错误的裁剪图像,即高度错误。我不明白我在哪里更改 original_image 的高度,因为 rectangle1 显示错误的高度。
我期待代码给我一个与原始图像具有相同高度的图像,但其宽度是原始图像的 1/3。
试过你的代码,但我没有发现任何错误。也许您的 x
和 y
在代码段中不为零。
添加cvWaitKey(0)
只是为了调试,对您的代码没有影响。
您的代码:
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/nonfree/features2d.hpp>
#include <opencv2/nonfree/nonfree.hpp>
#include <opencv2/opencv.hpp>
using namespace std;
int main(){
IplImage *original_image = cvLoadImage("C:/1.jpg", 0);
cvShowImage("orig", original_image);
int x = 0;
int y = 0;
int resultant_rectangle_width = ((int)(original_image->width/3));
int resultant_rectangle_height = (int)original_image->height;
//Remember that (x,y) is the coordination of the up-left corner.
//Official Document: http://docs.opencv.org/2.4.9/modules/core/doc/basic_structures.html
cvSetImageROI(original_image, cvRect(x, y,resultant_rectangle_width,resultant_rectangle_height));
//this cout block was to check if the ROI was set on original_image,but it showed the original_image properties here.
cout<<"original_image width after setting roi:"<<original_image->width<<endl;
cout<<"original_image height after setting roi:"<<original_image->height<<endl;
cvShowImage("3", original_image);
cvWaitKey(0);
//copying the ROI to another image
IplImage *rectangle1 = cvCreateImage(cvGetSize(original_image),original_image->depth,original_image->nChannels);
cvCopy(original_image, rectangle1);//using three arguments also did not help
cvResetImageROI(original_image);
cvShowImage("rectangle1", rectangle1);
cvWaitKey(0);
return 0;
}
结果:
整张图片
投资回报率
如你所见,他们确实一样高。因此,您很有可能错误地将 x
和 y
设置为某个错误的值。