This is the 19th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021″

Introduction to OpenCV

OpenCV is a cross-platform computer vision library based on BSD license (open source), its source code is mainly written by C and C++, support cross-platform compilation and operation. For example, it runs on Linux, Windows, Android and Mac OS operating systems. Because the source code is mainly WRITTEN in C and C++, the operation efficiency is very efficient; And OpenCV also provides Python, Ruby, MATLAB and other languages interface, cross-language call OpenCV is also very convenient. OpenCV contains many general algorithms for image processing and computer vision.

Ii. Problem scenario and cause description

In this article, we introduced how to call OpenCV in Qt. In Windows, using the MSVC compiler to call OpenCV will cause some functions in OpenCV to terminate abnormally. For example: When calling cascade classifier face_Cascade. detectMultiScale function for target classification detection, using MSVC compiler call will cause program abnormal crash, using OpencV call YOLO model for target detection, will also cause software crash; OpenCV is an official download library (version 3.4.7), using a 64-bit library, possibly because the version of the library compiler does not match the current MSVC compiler version, many online posts provide a solution is to use the current MSVC compiler to build a new library, I have not tested this; But running the same code with a MinGW compiler is fine, using the OpenCV version library that uses the same compiler currently in use, but not compiled myself, downloaded from here, link.juejin.cn/?target=htt… .

This also proves that the cause of the crash is not a code problem, but a mismatch between the OpenCV library and the current compiler. The solution is to either recompile the source code yourself, or find the corresponding OpenCV library for the compiler.

Here are some images of OpenCV using the YOLO V3 model:

Mat frame,frame_src,blob;
int inpWidth, inpHeight;

string names_file = "yolov3/yolo.names";
string model_def = "yolov3/yolov3.cfg";
string weights = "yolov3/yolov3.weights";

double thresh = 0.5;
double nms_thresh = 0.4; / / 0.4 0.25
inpWidth = inpHeight = 320; / / 416 608

//read names
ifstream ifs(names_file.c_str());
string line;
while (getline(ifs, line))classes.push_back(line);

// Initialize the model
Net net = readNetFromDarknet(model_def, weights);
net.setPreferableBackend(DNN_BACKEND_OPENCV);
net.setPreferableTarget(DNN_TARGET_CPU);
Copy the code

Three, OpenCV a few simple case usage

3.1 Use OpencV to read video frames

VideoCapture capture;
capture.open("D:/linux-share-dir/video-01.avi");
if(! capture.isOpened())
{
  qDebug() < <"can't open video";
  return;
}
Mat frame;
while(capture.read(frame))
{
  /* Displays recognition results on the control */
  //ui->label_2->setPixmap(QPixmap::fromImage(Mat2QImage(frame)));
  imshow("img",frame);
  qDebug() < <"1";
}
Copy the code

3.2 Turn the picture into grayscale image

#include <cv.h>  
#include <cxcore.h>  
#include <highgui.h>  
          
int main(int argc,char **argv)  
{  
    CvCapture* capture = NULL;  
    IplImage* pImg = NULL; 
    IplImage* pImg1 = NULL; 
    pImg = cvLoadImage(argv[1].1);
    pImg1 = cvCreateImage(cvSize(pImg->width,pImg->height),IPL_DEPTH_8U,1);
    cvCvtColor(pImg,pImg1,CV_RGB2GRAY);
    cvSaveImage("a.bmp",pImg1);
    cvSaveImage("a.jpg",pImg1);
    cvSaveImage("a.png",pImg1);
    cvReleaseImage( &pImg );   
    cvReleaseImage( &pImg1 );   
    return 0;  
}
Copy the code

3.3 Python calls OpencV to open an image display

import cv2
# loading images
src=cv2.imread("D:/linux-share-dir/888.jpg")
The first parameter is the window name, and the second parameter indicates that the window size can be adjusted
cv2.namedWindow("input image",cv2.WINDOW_NORMAL);
# display images
cv2.imshow("input image",src);
cv2.waitKey(0);
Copy the code

3.3 Calling OpencV in Python to enable real-time camera display

import numpy as np
import cv2
# call the laptop built-in camera, so the parameter is 0, if there are other cameras can be adjusted to 1,2
cap=cv2.VideoCapture(0)
while True:
    # Read pictures from camera
    sucess,img=cap.read()
    # Transform to grayscale image
    #gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    # Show camera, background is grayscale.
    #cv2.imshow("img",gray)
    cv2.imshow("img",img) # Display color image
    # Keep the picture going.
    k=cv2.waitKey(1)
    if k == 27:
        Exit camera by pressing esc
        cv2.destroyAllWindows()
        break
    elif k==ord("s") :Press the S key to save the image and exit.
        cv2.imwrite("image2.jpg",img)
        cv2.destroyAllWindows()
        break
# Turn off camera
cap.release()
Copy the code