Creating a OpenCV 2.3.1 application using QT
1.本次環境選用 Ubuntu 10.10、OpenCV 2.3.1 、cuda 4.0、QT 4.6
首先開啟QT介面
2.利用QT開啟新專案
File->New File or Project
3.建立QT Console Application
4.建立專案名稱及存放路徑在新建視窗中點選QT C++ Project,再點選建立QT Console Application
5.專案設置部分在此先不設立,直接點選Finish完成在這裡建議建立下面三個資料夾/home/trong/QT/QT_opencv_test//home/trong/QT/QT_opencv_test/QT_opencv_release/home/trong/QT/QT_opencv_test/QT_openccv_debug並將上面位置修改成Name: QT_opencv_testCreate in: /home/trong/QT/QT_opencv_test
6.在QT中設置開發環境
7.利用Opencv 讀取影像開啟影像之動作當建立專案檔之後,其環境設定檔將為"檔名.pro"檔案,你必須要此設立相關include路徑,及lib路徑等,在opencv教學文件中提到,只要設輸入下列兩行,將可自動呼叫系統變數,並將需要使用之函數加入,但不知該版本的問題還是哪邊設定出了錯,依然無法使用該方式加入,因此只好手動將需要之include路徑及lib路徑輸入,如下圖所示。CONFIG += link_pkgconfigPKGCONFIG += opencv上述設定如下表所示
CONFIG += link_pkgconfig
PKGCONFIG += opencv
INCLUDEPATH += /usr/local/opencv/include
LIBS += -L/usr/local/opencv/lib -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_flann -lopencv_gpu
8.在QT中建立Build資料夾上述程式碼如下表
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
int main()
{
// read an image
cv::Mat image= cv::imread("beach.jpg");
// create image window named "My Image"
cv::namedWindow("My Image");
// show the image on window
cv::imshow("My Image", image);
// wait key for 5000 ms
cv::Mat result;
cv::flip(image,result,1);
cv::namedWindow("result image");
cv::imshow("result image",result);
cv::waitKey(5000);
return 1;
}
9.Build C++ 程式QT中你必須設定一個Build的資料夾,這個資料夾位置不可與專案檔在一起,因此自行建立一個資料夾供該Build使用Build directoryRelease:/home/trong/QT/QT_opencv_test/QT_opencv_releaseDebug:/home/trong/QT/QT_opencv_test/QT_openccv_debug
建立好的檔案如下圖所示當編寫好程式碼之後發現無任何錯誤,可使用下圖進行Build動作
10.執行該程式
你可以直接點選該執行檔進行檔案執行,執行結果如下圖所示
11.本次OpenCV所使用的相關函數
imreadLoads an image from a file.C++: Mat imread(const string& filename, int flags=1 )Python: cv2.imread(filename[, flags ]) → retvalC: IplImage* cvLoadImage(const char* filename, int flags=CV_LOAD_IMAGE_COLOR ) C: CvMat* cvLoadImageM(const char* filename, int flags=CV_LOAD_IMAGE_COLOR ) Python: cv.LoadImage(filename, flags=CV_LOAD_IMAGE_COLOR) → NonePython: cv.LoadImageM(filename, flags=CV_LOAD_IMAGE_COLOR) → NoneParametersfilename – Name of file to be loaded.flags – Flags specifying the color type of a loaded image:– >0 Return a 3-channel color image– =0 Return a grayscale image– <0 Return the loaded image as is. Note that in the current implementation the alpha channel, if any, is stripped from the output image. For example, a 4-channel RGBA image is loaded as RGB if flags ≥ 0 .The function imread loads an image from the specified file and returns it. If the image cannot be read (be- cause of missing file, improper permissions, unsupported or invalid format), the function returns an empty matrix ( Mat::data==NULL ). Currently, the following file formats are supported:• Windows bitmaps - *.bmp, *.dib (always supported)• JPEG files - *.jpeg, *.jpg, *.jpe (see the Notes section)• JPEG 2000 files - *.jp2 (see the Notes section)• Portable Network Graphics - *.png (see the Notes section)• Portable image format - *.pbm, *.pgm, *.ppm (always supported)• Sun rasters - *.sr, *.ras (always supported)• TIFF files - *.tiff, *.tif (see the Notes section)
Note:• The function determines the type of an image by the content, not by the file extension.• On Microsoft Windows* OS and MacOSX*, the codecs shipped with an OpenCV image (libjpeg, libpng, libtiff, and libjasper) are used by default. So, OpenCV can always read JPEGs, PNGs, and TIFFs. On MacOSX, there is also an option to use native MacOSX image readers. But beware that currently these native image loaders give images with different pixel values because of the color management embedded into MacOSX.• On Linux*, BSD flavors and other Unix-like open-source operating systems, OpenCV looks for codecs supplied with an OS image. Install the relevant packages (do not forget the development files, for example, “libjpeg-dev”, in Debian* and Ubuntu*) to get the codec support or turn on the OPENCV_BUILD_3RDPARTY_LIBS flag in CMake.
namedWindowCreates a window.C++: void namedWindow(const string& winname, int flags)Python: cv2.namedWindow(winname[, flags ]) → NoneC: int cvNamedWindow(const char* name, int flags)Python: cv.NamedWindow(name, flags=CV_WINDOW_AUTOSIZE) → NoneParametersname – Name of the window in the window caption that may be used as a window identifier.flags – Flags of the window. Currently the only supported flag is CV_WINDOW_AUTOSIZE. If this is set, the window size is automatically adjusted to fit the displayed image (seeimshow() ), and you cannot change the window size manually.The function namedWindow creates a window that can be used as a placeholder for images and trackbars. Created windows are referred to by their names.If a window with the same name already exists, the function does nothing.You can call destroyWindow() or destroyAllWindows() to close the window and de-allocate any associated mem- ory usage. For a simple program, you do not really have to call these functions because all the resources and windows of the application are closed automatically by the operating system upon exit.
Note: Qt backend supports additional flags:• CV_WINDOW_NORMAL or CV_WINDOW_AUTOSIZE: CV_WINDOW_NORMAL enables you to resize the window, whereas CV_WINDOW_AUTOSIZE adjusts automatically the window size to fit the displayed image (see imshow() ), and you cannot change the window size manually.• CV_WINDOW_FREERATIO or CV_WINDOW_KEEPRATIO: CV_WINDOW_FREERATIO adjusts the image with no respect to its ratio, whereas CV_WINDOW_KEEPRATIO keeps the image ratio.• CV_GUI_NORMAL or CV_GUI_EXPANDED: CV_GUI_NORMAL is the old way to draw the window without statusbar and toolbar, whereas CV_GUI_EXPANDED is a new enhanced GUI.By default, flags == CV_WINDOW_AUTOSIZE | CV_WINDOW_KEEPRATIO | CV_GUI_EXPANDED
imshowDisplays an image in the specified window.C++: void imshow(const string& winname, InputArray image)Python: cv2.imshow(winname, image) → NoneC: void cvShowImage(const char* winname, const CvArr* image)Python: cv.ShowImage(winname, image) → NoneParameterswinname – Name of the window.image – Image to be shown.The function imshow displays an image in the specified window. If the window was created with the CV_WINDOW_AUTOSIZE flag, the image is shown with its original size. Otherwise, the image is scaled to fit the window. The function may scale the image, depending on its depth:• If the image is 8-bit unsigned, it is displayed as is.• If the image is 16-bit unsigned or 32-bit integer, the pixels are divided by 256. That is, the value range[0,255*256] is mapped to [0,255].• If the image is 32-bit floating-point, the pixel values are multiplied by 255. That is, the value range [0,1] ismapped to [0,255].
waitKeyWaits for a pressed key.C++: int waitKey(int delay=0)Python: cv2.waitKey([delay ]) → retvalC: int cvWaitKey(int delay=0 )Python: cv.WaitKey(delay=0) → intParametersdelay – Delay in milliseconds. 0 is the special value that means “forever”.The function waitKey waits for a key event infinitely (when delay ≤ 0 ) or for delay milliseconds, when it is positive. Since the OS has a minimum time between switching threads, the function will not wait exactly delay ms, it will wait at least delay ms, depending on what else is running on your computer at that time. It returns the code of the pressed key or -1 if no key was pressed before the specified time had elapsed.the pressed key or -1 if no key was pressed before the specified time had elapsed.
Note: This function is the only method in HighGUI that can fetch and handle events, so it needs to be calledperiodically for normal event processing unless HighGUI is used within an environment that takes care of event processing.
Note: The function only works if there is at least one HighGUI window created and the window is active. If there areseveral HighGUI windows, any of them can be active.
flipFlips a 2D array around vertical, horizontal, or both axes.C++: void flip(InputArray src, OutputArray dst, int flipCode)Python: cv2.flip(src, flipCode[, dst ]) → dstC: void cvFlip(const CvArr* src, CvArr* dst=NULL, int flipMode=0)Python: cv.Flip(src, dst=None, flipMode=0) → NoneParameterssrc – Source array.dst – Destination array of the same size and type as src .flipCode – Flag to specify how to flip the array. 0 means flipping around the x-axis. Positive value (for example, 1) means flipping around y-axis. Negative value (for example, -1) means flipping around both axes. See the discussion below for the formulas.The function flip flips the array in one of three different ways (row and column indices are 0-based):The example scenarios of using the function are the following:• Vertical flipping of the image (flipCode == 0) to switch between top-left and bottom-left image origin. This is a typical operation in video processing on Microsoft Windows* OS.• Horizontal flipping of the image with the subsequent horizontal shift and absolute difference calculation to check for a vertical-axis symmetry (flipCode > 0).• Simultaneous horizontal and vertical flipping of the image with the subsequent shift and absolute difference calculation to check for a central symmetry (flipCode < 0).• Reversing the order of point arrays (flipCode > 0 or flipCode == 0).See Also:transpose() , repeat() , completeSymm()
沒有留言:
張貼留言