반응형

It describes how to detect faces with Haar-cascade classifier.

OpenCV에서 Haar-cascade classifier를 이용한 얼굴 검출.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <opencv2/opencv.hpp>
 
using namespace std;
using namespace cv;
 
int main()
{
    Mat src = imread("girl.jpg");
 
    if (src.empty()) {
        cerr << "Image load failed." << endl;
 
        return -1;
    }
 
    CascadeClassifier classifier("haarcascade_frontalface_default.xml");
    // Cascade classifier class for object detection.
 
    if (classifier.empty()) {
        cerr << "Classifier load failed." << endl;
 
        return -1;
    }
 
    vector<Rect> faces;
    classifier.detectMultiScale(src, faces);
    // Detects objects of different sizes in the input image.
    // The detected objects are returned as a list of rectangles.
 
    for (Rect rect : faces) {
        rectangle(src, rect, Scalar(00255), 2);
    }
 
    imshow("Face Detection", src);
 
    waitKey(0);
 
    return 0;
}



Run the program and see the result.


Detected two babies' faces.


It couldn't detect a face with shades and detected some wrong objects.


반응형
Posted by J-sean
:
반응형

It shows how to detect and decode a QR Code.

OpenCV에서 QR코드를 감지하고 내용을 확인할 수 있다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <Windows.h>
#include <opencv2/opencv.hpp>
 
using namespace std;
using namespace cv;
 
int main()
{
    VideoCapture cap(0);
 
    if (!cap.isOpened()) {
        cerr << "Camera open failed." << endl;
 
        return -1;
    }
 
    QRCodeDetector detector;
 
    Mat frame;
    vector<Point> points;
    String msg;
 
    while (true) {
        cap >> frame;
 
        if (frame.empty()) {
            cerr << "Empty frame." << endl;
 
            break;
        }
 
        msg = detector.detectAndDecode(frame, points); // Both detects and decodes QR code.
 
        if (!msg.empty()) {
            polylines(frame, points, true, Scalar(00255), 2);
            putText(frame, msg, Point(1030), FONT_HERSHEY_PLAIN, 2, Scalar(00255), 2);
 
            if (msg.substr(04== "http") {
                imshow("QR Code", frame);
 
                if (MessageBox(NULL, (msg + " is a website address.\nDo you want to visit?").c_str(), "QR Code", MB_YESNO) == IDYES) {
                    ShellExecute(NULL"open", msg.c_str(), NULLNULL, SW_SHOW);
                    // Performs an operation on a specified file.
                }
                else {
                    continue;
                }
            }
        }
        else {
            putText(frame, "No QR code detected", Point(1030), FONT_HERSHEY_PLAIN, 2, Scalar(00255), 2);
        }
 
        imshow("QR Code", frame);
 
        if (waitKey(10== 27)
            break;
    }
 
    return 0;
}



Run the program. It says 'No QR code detected' at first.


QR code with a message.


QR code with a website address.


If the first 4 letters of the message are 'http', it opens the website with the default web browser.



반응형
Posted by J-sean
:
반응형

Detect, compute and draw key points.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <opencv2/opencv.hpp>
 
using namespace std;
using namespace cv;
 
int main(int argc, char** argv)
{
    Mat src = imread("Source.jpg", IMREAD_GRAYSCALE);
    
    if (src.empty()) {
        cerr << "Image load failed!" << endl;
        
        return -1;
    }
 
    Ptr<Feature2D> feature = ORB::create();
    // static Ptr<ORB> cv::ORB::create(int nfeatures = 500, float scaleFactor = 1.2f, int nlevels = 8,
    // int edgeThreshold = 31, int firstLevel = 0, int WTA_K = 2, ORB::ScoreType scoreType = ORB::HARRIS_SCORE,
    // int patchSize = 31, int fastThreshold = 20)
 
    vector<KeyPoint> keypoints;
    feature->detect(src, keypoints);
    // Detects keypoints in an image (first variant) or image set (second variant).
 
    Mat desc;
    feature->compute(src, keypoints, desc);
    // Computes the descriptors for a set of keypoints detected in an image (first variant) or image set (second variant).
 
    cout << "keypoints.size(): " << keypoints.size() << endl;
    cout << "desc.size(): " << desc.size() << endl;
 
    Mat dst;
    drawKeypoints(src, keypoints, dst, Scalar::all(-1), DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
    // Draws keypoints.
 
    imshow("src", src);
    imshow("dst", dst);
 
    waitKey();
    
    return 0;
}




Sizes of keypoints and descriptors.


Source image.


Keypoints with sizes and orientations.


반응형
Posted by J-sean
:
반응형

GDI+ Bitmap class inherits from the Image class. The Image class provides methods for loading and saving vector images (metafiles) and raster images (bitmaps). You can build Windows applications with GDI+ and OpenCV.

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <windows.h>
#include <gdiplus.h>
#include <opencv2/opencv.hpp>
 
#pragma comment(lib, "gdiplus")
 
using namespace cv;
using namespace Gdiplus;
 
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HINSTANCE g_hInst;
LPCSTR lpszClass = "MatToBitmap";
 
Mat src;    // source image
Mat org;    // original image
Mat dst;    // processed image
 

Prepare necessities.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdParam, int nCmdShow)
{
    HWND hWnd;
    MSG Message;
    WNDCLASS WndClass;
    g_hInst = hInstance;
 
    ULONG_PTR gpToken;
    GdiplusStartupInput gpsi;
    if (GdiplusStartup(&gpToken, &gpsi, NULL!= Ok) {
        MessageBox(NULL, TEXT("GDI+ start-up error."), TEXT("GDI+ Error"), MB_OK);
 
        return 0;
    }
 
    WndClass.cbClsExtra = 0;
    WndClass.cbWndExtra = 0;
    WndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1); //(HBRUSH)GetStockObject(WHITE_BRUSH);
    WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
    WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    WndClass.hInstance = hInstance;
    WndClass.lpfnWndProc = (WNDPROC)WndProc;
    WndClass.lpszClassName = lpszClass;
    WndClass.lpszMenuName = NULL;
    WndClass.style = CS_HREDRAW | CS_VREDRAW;
    RegisterClass(&WndClass);
 
    hWnd = CreateWindow(lpszClass, lpszClass, WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, CW_USEDEFAULT,
        670340NULL, (HMENU)NULL, hInstance, NULL);
    ShowWindow(hWnd, nCmdShow);
 
    while (GetMessage(&Message, 000)) {
        TranslateMessage(&Message);
        DispatchMessage(&Message);
    }
 
    GdiplusShutdown(gpToken);
 
    return (int)Message.wParam;
}
 
 

Start GDI+ and change the background color to the radio button color and Window size.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
bool getFile(PTCHAR filename)
{
    OPENFILENAME ofn;
    memset(&ofn, 0sizeof(OPENFILENAME));
    ofn.lStructSize = sizeof(OPENFILENAME);
    ofn.hwndOwner = NULL;
    ofn.lpstrFilter = TEXT("all(*.*)\0*.*\0jpg(*.jpg)\0*.jpg\0png(*.png)\0*.png\0bmp(*.bmp)\0*.bmp\0");
    ofn.lpstrFile = filename;
    ofn.nMaxFile = MAX_PATH;
 
    if (GetOpenFileName(&ofn) != 0) {
        //MessageBox(NULL, filename, TEXT("File opened."), MB_OK);
 
        return true;
    }
    else {
        MessageBox(NULL, TEXT("File open failed"), TEXT("No file selected"), MB_OK);
 
        return false;
    }
}
 
 

Retrieve an image file name.

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
bool ImageLoad()
{
    TCHAR filename[MAX_PATH] = "";
 
    if (getFile(filename)) {
        src = imread(filename);
        if (src.empty()) {
            MessageBox(NULL, TEXT("Image load failed"), TEXT("No image loaded"), MB_OK);
 
            return false;
        }
 
        return true;
    }
    else {
        return false;
    }
}
 
 

Read the image from the file.

 

1
2
3
4
5
6
7
8
void ImageResize()
{
    // maximum image display size: 320 X 240 with original ratio
    double ratio = min((double)320 / (double)src.size().width, (double)240 / (double)src.size().height);
    resize(src, src, cv::Size(), ratio, ratio);
    org = src.clone();
    cvtColor(org, org, COLOR_BGR2BGRA);
}
 
 

Resize an image.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
void ImageProcess(int direction)
{
    cvtColor(src, dst, COLOR_BGR2GRAY);
 
    float vertical[] = {
        -202,
        -202,
        -202
    };
 
    float horizontal[] = {
        -2-2-2,
        000,
        222
    };
 
    float diagonal[] = {
        -2-20,
        -202,
        022
    };
    
    Mat emboss;
    switch (direction)
    {
    case 0:
        emboss = Mat(33, CV_32FC1, vertical);
 
        break;
 
    case 1:
        emboss = Mat(33, CV_32FC1, horizontal);
 
        break;
 
    case 2:
        emboss = Mat(33, CV_32FC1, diagonal);
 
        break;
 
    default:
        emboss = Mat(33, CV_32FC1, vertical);
    }
    filter2D(dst, dst, -1, emboss, cv::Point(-1-1), 128);
    
    cvtColor(dst, dst, COLOR_GRAY2BGRA);
}
 
 

Process an image with embossing filter.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void OnPaint(HDC hdc)
{
    if (!dst.empty()) {
        Graphics G(hdc);
 
        Bitmap bitmapDst(dst.size().width, dst.size().height, dst.step, PixelFormat32bppARGB, dst.data);
        Bitmap bitmapOrg(org.size().width, org.size().height, org.step, PixelFormat32bppARGB, org.data);
        // stride(src.step): Integer that specifies the byte offset between the beginning of one scan line and
        // the next. This is usually(but not necessarily) the number of bytes in the pixel format(for example,
        // 2 for 16 bits per pixel) multiplied by the width of the bitmap. The value passed to this parameter
        // must be a multiple of four.
 
        G.DrawImage(&bitmapDst, 00, bitmapDst.GetWidth(), bitmapDst.GetHeight());
        G.DrawImage(&bitmapOrg, bitmapOrg.GetWidth() + 100, bitmapOrg.GetWidth(), bitmapOrg.GetHeight());
    }
    else {
        TextOut(hdc, 270110, TEXT("No image to display"), 19);
    }
}
 
 

Draw original and processed images.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
enum { ID_R1 = 101, ID_R2, ID_R3 };
HWND r1, r2, r3;
int x = 10;
int y = 270;
int w = 90;
int h = 20;
 
LRESULT CALLBACK WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam)
{
    HDC hdc;
    PAINTSTRUCT ps;    
 
    switch (iMessage) {
    case WM_CREATE:
        if (ImageLoad()) {
            ImageResize();
            ImageProcess(0);
        }
        
        CreateWindow(TEXT("button"), TEXT("Filter type"), WS_CHILD | WS_VISIBLE |
            BS_GROUPBOX, 525029050, hWnd, (HMENU)0, g_hInst, NULL);
 
        r1 = CreateWindow(TEXT("button"), TEXT("Vertical"), WS_CHILD | WS_VISIBLE |
            BS_AUTORADIOBUTTON | WS_GROUP, x, y, w, h, hWnd, (HMENU)ID_R1, g_hInst, NULL);
 
        r2 = CreateWindow(TEXT("button"), TEXT("Horizontal"), WS_CHILD | WS_VISIBLE |
            BS_AUTORADIOBUTTON, x + 90, y, w, h, hWnd, (HMENU)ID_R2, g_hInst, NULL);
 
        r3 = CreateWindow(TEXT("button"), TEXT("Diagonal"), WS_CHILD | WS_VISIBLE |
            BS_AUTORADIOBUTTON, x + 180, y, w, h, hWnd, (HMENU)ID_R3, g_hInst, NULL);
 
        CheckRadioButton(hWnd, ID_R1, ID_R3, ID_R1);
 
        return 0;
 
    case WM_COMMAND:
        if (!dst.empty()) {
            switch (LOWORD(wParam)) {
            case ID_R1:
                ImageProcess(0);
                break;
 
            case ID_R2:
                ImageProcess(1);
                break;
 
            case ID_R3:
                ImageProcess(2);
                break;
            }
 
            InvalidateRect(hWnd, NULL, TRUE);
        }
 
        return 0;
 
    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);
        OnPaint(hdc);
        EndPaint(hWnd, &ps);
 
        return 0;
 
    case WM_DESTROY:
        PostQuitMessage(0);
 
        return 0;
    }
 
    return(DefWindowProc(hWnd, iMessage, wParam, lParam));
}
 
 

Define the necessary variables and handle window messages.

 

 

Run the application and select an image file.

 

Processed and original image with a vertical filter.

 

Processed and original image with a horizontal filter.

 

Processed and original image with a diagonal filter.

 

 

반응형
Posted by J-sean
:
반응형

Detects motions in the vision and saves an image of it as a PNG file. Adjust the sensitivity value if needed.


카메라 영상의 움직임을 감시하고 움직임이 감지된 순간의 영상을 저장한다. sensitivity 값으로 감도를 조절한다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <opencv2/opencv.hpp>
 
using namespace std;
using namespace cv;
 
int main(int argc, char** argv)
{
    VideoCapture cap(0);
 
    Mat frameNew;
    Mat frameOld;
    Mat frameDiff;
 
    double min, max;
    int sensitivity = 100;
    int detectionCount = 0;
 
    cap >> frameOld;
 
    while (true)
    {
        cap >> frameNew;
        if (frameNew.empty())
            break;
 
        // Calculates the per-element absolute difference
        // between two arrays or between an array and a scalar.
        absdiff(frameNew, frameOld, frameDiff);
        cvtColor(frameDiff, frameDiff, COLOR_BGR2GRAY);
        minMaxLoc(frameDiff, &min, &max);
 
        if (max > sensitivity)
        {
            cout << "Motion detected. (Max: " << max << ")" << endl;
 
            // For PNG, it can be the compression level from 0 to 9.
            // A higher value means a smaller size and longer compression time.
            vector<int> compression_params;
            compression_params.push_back(IMWRITE_PNG_COMPRESSION);
            compression_params.push_back(3);
            if (imwrite(format("detection_%03d.png", detectionCount++), frameNew, compression_params))
                cout << "Image saved." << endl;
            else
                cout << "Image not saved." << endl;
        }
 
        imshow("Motion Detectoion", frameDiff);
        
        frameNew.copyTo(frameOld);
 
        if (waitKey(10== 27)
            break;
    }
 
    return 0;
}
cs



<frameDiff>


<Detection_XXX.png>




반응형
Posted by J-sean
:
반응형

Detecting a semiconductor wafer by Hough transform.

원형인 반도체 Wafer를 감지 한다. 배경에 노이즈가 많으면 오감지 가능성이 커진다.


작성 중.............



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <opencv2/opencv.hpp>
 
using namespace std;
using namespace cv;
 
int main(int argc, char** argv)
{
    VideoCapture cap(0);
 
    if (!cap.isOpened()) {
        cerr << "Camera open failed." << endl;
 
        return 0;
    }
    
    int frameWidth = (int)cap.get(CAP_PROP_FRAME_WIDTH);
    int frameHeight = (int)cap.get(CAP_PROP_FRAME_HEIGHT);
 
    Mat frame, grayed, blurred;
    
    string textWafer = "Wafer detected";
    string textNoWafer = "No wafer detected";
    // Size cv::getTextSize(const String & text, int fontFace, double fontScale, int thickness,    int* baseLine)
    Size textSizeWafer = getTextSize(textWafer, FONT_HERSHEY_SIMPLEX, 12NULL);
    Size textSizeNoWafer = getTextSize(textNoWafer, FONT_HERSHEY_SIMPLEX, 12NULL);
 
    while (true) {
        cap >> frame;
        if (frame.empty())
            break;
        
        cvtColor(frame, grayed, COLOR_BGR2GRAY);
        blur(grayed, blurred, Size(33));
        //GaussianBlur(grayed, blurred, Size(), 1.0);
        
        vector<Vec3f> circles;
        // void cv::HoughCircles(InputArray image, OutputArray circles, int method, double dp, double minDist, double param1 = 100, double param2 = 100, int minRadius = 0, int maxRadius = 0)
        // param1 - First method-specific parameter. In case of HOUGH_GRADIENT , it is the higher threshold of the two passed to the Canny edge detector (the lower one is twice smaller).
        // param2 - Second method - specific parameter.In case of HOUGH_GRADIENT, it is the accumulator threshold for the circle centers at the detection stage.The smaller it is, the more
        //            false circles may be detected.Circles, corresponding to the larger accumulator values, will be returned first.
        HoughCircles(blurred, circles, HOUGH_GRADIENT, 13001006050);
 
        if (circles.empty()) {
            putText(frame, textNoWafer, Point(cvRound(frameWidth / 2 - textSizeNoWafer.width / 2), cvRound(frameHeight / 2 + textSizeNoWafer.height / 2)), FONT_HERSHEY_SIMPLEX, 1, Scalar(00255), 2);
            imshow("frame", frame);
 
            if (waitKey(10== 27)
                break;
 
            continue;
        }
 
        for (Vec3f c : circles) {
            Point center(cvRound(c[0]), cvRound(c[1]));
            int radius = cvRound(c[2]);
 
            circle(frame, center, radius, Scalar(00255), 2);
            circle(frame, center, 2, Scalar(25500), 2);
            // void cv::putText(InputOutputArray img, const String & text, Point org, int fontFace, double fontScale, Scalar color, int thickness = 1, int lineType = LINE_8, bool bottomLeftOrigin = false)
            putText(frame, textWafer, Point2i(center.x - textSizeWafer.width / 2, center.y + textSizeWafer.height / 2), FONT_HERSHEY_SIMPLEX, 1, Scalar(02550), 2);
            //cout << "Wafer radius: " << radius << endl;
        }
 
        imshow("frame", frame);
 
        if (waitKey(10== 27)
            break;
    }
    
    return 0;
}












1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import cv2
 
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
 
frame_size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
print('frame_size =', frame_size)
 
text = 'Wafer detected'
textSize, baseLine = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, 12)
 
while True:
    retval, frame = cap.read()
    if not retval:
        break
    blured = cv2.GaussianBlur(frame, ksize = (1515), sigmaX = 10.0)
    # Blurs an image using a Gaussian filter.
    grayed = cv2.cvtColor(blured,cv2.COLOR_BGR2GRAY)
    circles = cv2.HoughCircles(grayed, method = cv2.HOUGH_GRADIENT, dp = 1, minDist = 300,
                            param2 = 15, minRadius = 50)
    # Finds circles in a grayscale image using the Hough transform.
    if circles is None:
        cv2.imshow('Wafer detection', frame)
        key = cv2.waitKey(25)
        if key == 27# ESC
            break
        continue
 
    for circle in circles[0, :]:    
        cx, cy, r = circle
        cv2.circle(frame, (cx, cy), r, (00255), 2)    # Wafer
        cv2.circle(frame, (cx, cy), 2, (25500), 2)    # Center
 
        textX = int(cx - textSize[0/ 2)
        textY = int(cy + textSize[1/ 2)
        cv2.putText(frame, text, (textX, textY), cv2.FONT_HERSHEY_SIMPLEX, 1, (02550), 2)
        
    cv2.imshow('Wafer detection', frame)
 
    key = cv2.waitKey(25)
    if key == 27# ESC
        break
 
 
if cap.isOpened():
    cap.release()
cv2.destroyAllWindows()
cs


반응형

'OpenCV' 카테고리의 다른 글

OpenCV Graphic User Interface(GUI) with cvui  (0) 2019.07.01
How to extract video from a video file 영상 추출 하기  (0) 2019.06.29
Video Analysis - Optical Flow  (0) 2019.01.03
tkinter GUI  (0) 2018.12.29
Video Analysis - Meanshift and CAMshift  (0) 2018.12.27
Posted by J-sean
: