opencv-stage1
openCV
DAY ONE
Load, change and save pictures.
- LOAD IMAGE -> cv::imread
- CHANGE IMAGE -> cv::cvtColor
- SAVE IMAGE -> cv::imwrite
cv::imread
Function:
Load a picture file as a ‘Mat’ object.
Arguments:
FIRST: image file name
SECOND: type of current image file
Types:
IMREAD_UNCHANGED(<0) -> load the origin image, no any changes.
IMREAD_GRAYSCALE(0) -> load the gray scale image.
IMREAD_COLOR(>0) -> load the RGB image. (DEFAULT)
For example:
1 | void easyimg(){ |
cv::namedWindow
Function:
Create an opencv window which is created and released automatically, we don’t need to destroy it.
Usage:
namedWindow(“Window Title”, WINDOW_AUTOSIZE);
WINDOW_AUTOSIZE will auto resize the window according to the size of image, but cannot be resized by manually.
WINDOW_NORMAL when use it and integrated with QT, it can be adjusted the window size.
cv::imshow
Function:
Display the image to the window according to the window title.
Arguments:
FIRST: window title
SECOND: Mat Object
cv::cvtColor
Function:
It can switch the color space to the another, it has three argument.
Arguments:
FIRST: origin image
SECOND: changed image
THREE: origin and color space of target, for example, COLOR_BGR2HLS , COLOR_BGR2GRAY etc.
Usage:
cvtColor(image, gray_image, COLOR_BGR2GRAY);
cv::imwrite
Function:
Save current image file to the fixed path.
Only the BGR image of png, jpg, tiff file of 8 bit or 16 bit which is single channel or three channels can be saved by this way.
When saving the png file, it can save the transparency channel images.
We can specify the compress arguments.
Arguments:
FIRST: output file path and name
SECOND: the Mat Object which will be saved
DAY TWO
Get the pointer of image pixel.
CV_Assert
CV_Assert(myImage.depth() == CV_8U);
Mat.ptr<uchar>(int i=0) -> Get the pointer of pixel matrix, the index i express the number of lines, count from zero.
const uchar* current=myImage.ptr<uchar>(row); -> Get the current line pointer.
p(row, col)=current[col] -> Get the value of pixel of current pixel point P(row, col).


