Skip to content

OpenCV:IplImage

OpenCV에서 사용하는 IplImage에 대한 내용을 정리한다.

Classess

IplImage vc cv::Mat

IplImage

Mat

description

nChannels

Mat::channels()

이미지 채널의 개수

width

Mat::cols

가로 픽셀의 개수

height

Mat::rows

세로 픽셀의 개수

imageData

Mat::data

실제 이미지의 첫 픽셀에 대한 포인터

widthStep

Mat::step1()

가로로 된 한 라인의 바이트

imageSize

width * height * nChannels |- 

Drawing shapes in OpenCV

#include <cv.h>
#include <highgui.h>
int main()
{
    // Window to display shapes
    cvNamedWindow("Shapes",CV_WINDOW_AUTOSIZE);
    // Image structure
    IplImage* shape=cvLoadImage("Tulips.jpg");

    // clear or initialize the image to scalar
    cvSet(img, cvScalar(0,0,0));

    // Initializing font
    CvFont font;
    cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 1.0, 1.0, 0, 3, CV_AA);
    // Drawing a Line
    cvLine(shape,cvPoint(100,100),cvPoint(200,100),CV_RGB(0,255,0),10,8);
    // Putting Text 'Line'
    cvPutText(shape,"Line", cvPoint(100,80), &font, cvScalar(255,0,0));

    // Drawing a Rectangle
    cvRectangle(shape,cvPoint(100,150),cvPoint(300,300),CV_RGB(0,255,0),5,8);
    // Putting Text 'Rectangle'
    cvPutText(shape,"Rectangle", cvPoint(100,140), &font, cvScalar(255,0,0));

    // Drawing a Circle
    cvCircle(shape,cvPoint(500,150),50,CV_RGB(0,255,0),-1);
    // Putting Text 'Circle'
    cvPutText(shape,"Circle", cvPoint(450,90), &font, cvScalar(255,0,0));

    // Showing the image
    cvShowImage("Shapes",shape);

    // Escape Sequence
    cvWaitKey(0);

    // Save image.
    cvSaveImage("foo.png", shape);

    // CleanUp
    cvReleaseImage(&shape);
    cvDestroyAllWindows();
}

Drawing center position text

그림의 중심에 문자열을 그리는 방법은 아래와 같다.

void GlobalEngine::createEmptyImage()
{
    int const width = 300;
    int const height = 300;
    std::string const text = "Empty image";

    _empty_image.reset(cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 3), [](IplImage * image) {
        cvReleaseImage(&image);
    });

    CvFont font;
    CvSize size;
    cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 1.0, 1.0, 0, 3, CV_AA);
    cvGetTextSize(text.c_str(), &font, &size, nullptr);
    CvPoint point = cvPoint((width - size.width) / 2, (height - size.height) / 2);

    cvRectangle(_empty_image.get(), cvPoint(0, 0), cvPoint(width, height), CV_RGB(0, 255, 0), -1);
    cvPutText(_empty_image.get(), text.c_str(), point, &font, CV_RGB(255, 255, 255));
}

Mat & CvMat & IplImage, CvArr convert function

// IplImage* -> cv::Mat
Mat Mat_img(IplImage_img);
Mat Mat_img = cvarrToMat(IplImage_img);

// IplImage* -> CvMat
cvGetMat( IplImage_img, &CvMat_img);

// cv::Mat -> CvMat
CvMat CvMat_img = Mat_img; // convert directly

// CvMat -> IplImage*
IplImage *IplImage_img = cvGetImage(CvMat_img);

// CvMat -> cv::Mat
Mat Mat_img(CvMat_img);

cv::Mat to IplImage

cv::Mat source_mat_image;
// edit source_mat_image ...
IplImage * copy_ipl_image = cvCreateImage(cvSize(source_mat_image.cols, source_mat_image.rows), IPL_DEPTH_8U, 1);
IplImage temp = source_mat_image;
{
    // WARNING: source_mat_image의 색상 깊이 등을 정확히 파악한 후 복사해야 한다.
    cvCopy(&temp, _foreground_ipl.get());
    // or // cvCvtColor(&temp, _foreground_ipl.get(), CV_BGR2GRAY);
}
cvReleaseImage(&copy_ipl_image);

OpenCV 3.x version

아래와 같은 OpenCV 2.x 버전의 아래 코드는 OpenCV 3.x 버전에서 정상적으로 작동하지 않는다.

IplImage * ipl1, *ipl2;
// ...
const cv::Mat m = cv::Mat(ipl,false);  // Fails 
cv::Mat  m2 = ipl2;  // Fails

아래와 같이 변경하면 된다.

IplImage * ipl = ...;
cv::Mat m = cv::cvarrToMat(ipl);  // default additional arguments: don't copy data.

Writing to IplImage image data

If you are looking at frame->imageSize keep in mind that it is frame->height * frame->widthStep, NOT frame->height * frame->width.

BGR is the native format of OpenCV, not RGB.

Also, if you're just getting started, you should consider using the C++ interface (where Mat replaces IplImage) since that is the future direction and it's a lot easier to work with.

Here's some sample code that accesses pixel data directly:

#include "opencv/cv.h"
#include "opencv/highgui.h"
#include "opencv/cxcore.h"

int main()
{
    IplImage *frame = cvCreateImage(cvSize(41, 41), IPL_DEPTH_8U, 3);

    for (int y = 0; y < frame->height; y++) {
        uchar* ptr = (uchar*) (frame->imageData + y * frame->widthStep);
        for (int x = 0; x < frame->width; x++) {
            ptr[3 * x + 2] = 255; //Set red to max (BGR format)
        }
    }

    cvNamedWindow("window", CV_WINDOW_AUTOSIZE);
    cvShowImage("window", frame);
    cvWaitKey(0);
    cvReleaseImage(&frame);
    cvDestroyWindow("window");
    return 0;
}

Crop image

IplImage orig = cvLoadImage("orig.png");
CvRect r = cvRect(100, 100, 200, 200); //Creating rectangle by which bounds image will be cropped
cvSetImageROI(orig, r); //After setting ROI (Region-Of-Interest) all processing will only be done on the ROI
IplImage * cropped = cvCreateImage(cvSize(orig->width, orig->height), orig->depth, orig->nChannels);
cvCopy(orig, cropped); //Copy original image (only ROI) to the cropped image
cvSaveImage("cropped.png", cropped);

Resize image

IplImage * createResizeImage(IplImage const * image, int width, int height)
{
    IplImage * destination = cvCreateImage(cvSize(width, height), image->depth, image->nChannels);
    cvResize(image, destination);
    return destination;
}

Convert RGB to Black & White in OpenCV

// C
IplImage *im_rgb  = cvLoadImage("image.jpg");
IplImage *im_gray = cvCreateImage(cvGetSize(im_rgb),IPL_DEPTH_8U,1);
cvCvtColor(im_rgb,im_gray,CV_RGB2GRAY);

// C++
Mat im_rgb  = imread("image.jpg");
Mat im_gray;
cvtColor(im_rgb,im_gray,CV_RGB2GRAY);

See also

Favorite site