2016-10-26 2 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

을 즉를 image에 대한 img1roi를 대체합니다. x=widthy=length부터 이미지의 외곽을 사용하고 있습니다. xy은 로이의 왼쪽 상단에 위치해야합니다. 이 경우 모두 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); 

당신은 구체적으로 확인할 수있는 subconditions는 디버거를 통해 각 코드를 확인하여 실패 주장의. 값을 출력하여 비교할 수도 있습니다. 문제는 당신 roi에 : 각각 rect에 대한

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

감사합니다. Opencv를 사용하고 방금 배움으로써 새로운 것입니다. –