2016-10-26 14 views
-1

画像の高さと幅を半分にして切り抜くことで、元の画像の半分にしようとしています。 [画像トリミングする] [1]opencvの選択されたピクセルによる画像の切り取り

以下は私のコードですが、それは実行時例外を引き起こし、.exeファイルは、以下のエラーで動作を停止します:

以下
OpenCV Error: Assertion failed (rect.width >= 0 && rect.height >= 0 && rect.x < image->width && rect.y < image->height && rect.x + rect.width >= (int)(rect.width > 0) && rect.y + rect.height >= (int)(rect.height > 0)) in cvSetImageROI, file C:\Development\opencv\sources\modules\core\src\array.cpp, line 3006 

This application has requested the Runtime to terminate it in an unusual way. 

Please contact the application's support team for more information. 

ですコード:

#include <iostream> 
#include <string> 
#include <opencv/cv.h> 
#include <opencv/cxcore.h> 
#include <opencv/highgui.h> 

using namespace std; 
using namespace cv; 

int main(int argc, char** argv) { 
    IplImage *img1 = cvLoadImage("image/testcase.jpg"); 
    cvNamedWindow("Image1:",1); 
    cvShowImage("Image1:",img1); 
    cout << "Width:" << img1->width <<" pixels"<< endl; 
    cout << "Height:" << img1->height <<" pixels"<< endl; 
    int width = img1->width ; 
    int lenght = img1->height; 

    // cropping the image 

    Rect roi; 
    roi.x = width; 
    roi.y = lenght; 
    roi.width = (roi.x)/2; 
    roi.height = (roi.y)/2; 

    Mat image_test; 
    image_test = imread("image/testcase"); 
    // Must have dimensions of output image 

    IplImage* cropped = cvCreateImage(cvSize(roi.width,roi.height), img1->depth, img1->nChannels); 

    cvSetImageROI(img1, roi); 
    cvCopy(img1, cropped); 
    cvResetImageROI(img1); 
    cvNamedWindow("Cropped Image", 1); 
    cvShowImage("Cropped Image", cropped); 
    cvSaveImage ("savedImage/cropped.jpg" , cropped); 
    waitKey(0); 
    return 0; 
} 
+1

をあなたは**時代遅れ使用している任意の特定の理由* * C api? – Miki

答えて

0

問題は、あなたのroiです。​​とy=length以降、あなたはイメージから外を使用しています。 xyはあなたのroiの左上隅にする必要があります。この場合、両方とも0である必要があります。

廃止されました C API。

画像の左上の部分の収穫を得る、あなたは、単に行うことができます

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

int main() 
{ 
    // Load image 
    Mat3b img = imread("path_to_image"); 

    // Define the roi 
    Rect roi(0, 0, img.cols/2, img.rows/2); 

    // Crop 
    Mat3b crop = img(roi); 

    // Show result 
    imshow("Original", img); 
    imshow("Crop", crop); 
    waitKey(); 

    return 0; 
} 

を生産:

enter image description here

+0

ありがとうございます。問題を解決しました。 –

0

(@Mikiで指摘したように)廃止されたAPIを使用して加えて、OpenCVのは、問題が(明確にするためにフォーマット)されて何を言っている:

OpenCV Error: Assertion failed 

(rect.width >= 0 
&& rect.height >= 0 
&& rect.x < image->width 
&& rect.y < image->height 
&& rect.x + rect.width >= (int)(rect.width > 0) 
&& rect.y + rect.height >= (int)(rect.height > 0)) 

in cvSetImageROI, file C:\Development\opencv\sources\modules\core\src\array.cpp, line 3006 

具体的には、cvSetImageROI()にこの呼び出しは失敗している:

cvSetImageROI(img1, roi); 

あなたのデバッガを経由して、それらの各々をチェックして失敗しているアサーション副条件のを特異的に決定することができます。また、値を出力して比較することもできます。すなわち、それぞれ、imagerectためroiためimg1に置き換え:

cout << roi.width << endl; // should be >= 0 
cout << roi.height << endl; // should be >= 0 
// etc 
+0

ありがとうございます。 Opencvを使って学習するだけで初めてです。 –

関連する問題