株式会社MITECH
Machine
Imagination Technologies
エム・アイ・テク

 

Useful OpenCV Sample Codes

IplImage

create an image ============
IplImage *imgLowSource = cvCreateImage(cvSize(width, height), 8, 1);
IplImage *img_in = cvLoadImage("landscape.jpg", 0);
cvShowImage("img_in", img_in);
waitKey(0);
============

access to pixels

============
IplImage* imgSource = NULL;

    if ((imgSource = cvLoadImage(filename, 0)) == 0)
{

    printf("File open error %s\n", fname);
}


// resize the image to standard size
IplImage *imgLowSource = cvCreateImage(cvSize(width, height), 8, 1);
cvResize(imgSource, imgLowSource, CV_INTER_LINEAR);
// get the data of the image

uchar* src = (uchar*)imgLowSource->imageData;
for (int j = 0; j < height; j++)

{
    for (int i = 0; i < width; i++)
    {
        float d = (float)((src + j*imgLowSource->widthStep)[i]);
        imgData[j*width + i] = d / 255.0f;
    }
}


============
access to pixels ============
((uchar *)(img->imageData + (j)*img->widthStep))[3*i] =
((uchar *)(img->imageData + (j)*img->widthStep))[3*i+1] =
((uchar *)(img->imageData + (j)*img->widthStep))[3*i+2] = 100;
============
clone a roi ============
IplImage *mapImg = cvCreateImage(cvSize(MapX, MapY), 8, 3);
IplImage *neuronImg = cvCreateImage(cvSize(sample_x*2, sample_y*2), 8, 3);
CvRect roi;
roi.x = 0; roi.y = 0;
roi.width = neuronImg->width; roi.height = neuronImg->height;

cvSetImageROI(mapImg, roi);
cvCopyImage(neuronImg, mapImg);
cvResetImageROI(mapImg);

cvShowImage("map", mapImg);
cvShowImage("neuron", neuronImg);

cvWaitKey(100);
============

clone a roi ============
// get image box
CvRect roi = cvRect(0, 0, BOX_SIZE, BOX_SIZE);
cvSetImageROI(img, roi);
cvSetImageROI(imgBox, cvRect(0,0, BOX_SIZE, BOX_SIZE));
cvCopy(img, imgBox)
============
save an image ============
cvSaveImage(path, img);
cvReleaseImage(&img);
============

print character ============
CvFont font;
cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5, 0, 1);

char info[] = "information";
cvPutText(img24bit, info, cvPoint(xTopLeft, yTopLeft), &font, cvScalar(0, 255, 255));
============

draw a rectangle ============
cvRectangle(img24bit, cvPoint(xTopLeft, yTopLeft), cvPoint(xTopLeft + w, yTopLeft + h), cvScalar(255, 0, 0), 2, 8, 0);
============

   

 

 

cv::Mat

create an image ============
cv::Mat img_gray (MAX_HEIGHT, MAX_WIDTH, CV_8UC1, cv::Scalar(255)); // 8 bit gray image

// convert gray image to color image
cv::cvtColor(img_gray, img_in_color, CV_GRAY2BGR);
cv::cvtColor(img_in_color, img_gray, CV_BGR2GRAY);

img_output = img_in_color.clone();
============

access to pixels ============
uchar *data = (uchar*)(img.data);
for(int j = 0;j < img.rows;j++){
    for(int i = 0;i < img.cols;i++){
        uchar b = data[img.step * j + i ] ;
        uchar g = data[img.step * j + i + 1];
        uchar r = data[img.step * j + i + 2];
    }
}
// đổi pixel đen thành trắng cho dễ nhìn
void cvt_black_white(Mat &bmp_in)
{
    uchar* imData = (uchar*)bmp_in.data;// lưu ý tùy vào ảnh mà uchar* hay int* hay float* ...
    for (int j = 0; j < bmp_in.rows; j++)
        for (int i = 0; i < bmp_in.cols; i++)
            if (imData[j*bmp_in.step + i] == 0)
            imData[j*bmp_in.step + i] = 255;
        }
    }
}
// dùng trực tiếp luôn
void cvt_black_white(Mat &bmp_in)
{
    for (int j = 0; j < bmp_in.rows; j++)
        for (int i = 0; i < bmp_in.cols; i++)
        {
            if (!bmp_in.data[j* bmp_in.cols + i])
            bmp_in.data[j* bmp_in.cols + i] = 255;
        }
    }
============
clone a roi ============
cv::Mat img_small;
img_in(cv::Rect(x, y, width, height)).copyTo(img_small);
============
Copy a small image to big image
cv::Mat img_small = cv::imread(filename, 1);
img_small.copyTo(img_out(cv::Rect(70, 640, img_small.cols, img_small.rows)));
save an image imwrite(fname, img_output);
resize ============
cv::Size size(227,227);//the dst image size,e.g.100x100
cv::Mat imgSized;
resize(imgIn,imgSized,size);//resize image
============

copy image data ============
// copy image data, dst should larger than src
inline void copyImgData(cv::Mat img_gray_dst, cv::Mat &img_gray_src)
{
    for (int j=0; j < img_gray_dst.rows; j++)
    {
        memset(((uchar *)(img_gray_dst.data + (j)*img_gray_dst.step)), 255, img_gray_dst.cols*sizeof(char));
        if (j < img_gray_src.rows)
            memcpy(((uchar *)(img_gray_dst.data + (j)*img_gray_dst.step)),
                   ((uchar *)(img_gray_src.data + (j)*img_gray_src.step)),
                   img_gray_src.cols*sizeof(char));
    }
    return;
}
============

print character ============
double fontScale = 3.0;
int thickness = 5;
char recog_char[] = "abc";
putText(img_output, recog_char, cv::Point(x,y), CV_FONT_HERSHEY_SIMPLEX, fontScale, cv::Scalar(0, 240, 120), thickness, CV_AA);
void putText(Mat& img, const string& text, Point org, int fontFace, double fontScale, Scalar color, int thickness=1, int lineType=8, bool bottomLeftOrigin=false )
============

draw a rectangle, circle ============
cv::Scalar color = cv::Scalar(0, 255, 51); // red color
cv::rectangle(img, cv::Point(x, y), cv::Point(x + width, y + height), color, 3, 8, 0);
============
void circle(Mat& img, Point center, int radius, const Scalar& color, int thickness=1, int lineType=8, int shift=0)
Some others OpenCV.cpp

 

 

Copyright©2016 Machine Imagination Technologies. All rights reserved.