반응형

Template matching is a technique for finding areas of an image that match (are similar) to a template image (patch).


OpenCV matchTemplate 함수와 threshold 값을 이용해 이미지에서 찾고 싶은 부분을 검색해 모두 찾을 수 있다.


2019/07/08 - [Software/OpenCV] - Template Matching(Image Searching) - 부분 이미지 검색

2019/07/12 - [Software/OpenCV] - Template Matching(Image Searching) with a mask for multiple objects - 마스크를 이용해 (배경이 다른) 반복되는 이미지 모두 찾기


<Target>


<Source>


Below code explains how to spot multiple objects with a threshold. Adjust threshold value if it doesn't work properly.

  • Type of the template matching operation: TM_SQDIFF_NORMED

  • Threshold: 0.00015

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
#include <opencv2/opencv.hpp>
#include <time.h>
 
using namespace cv;
using namespace std;
 
int main()
{
    clock_t start, end;
    double minVal;
    Point minLoc;
    double threshold = 0.00015;
    int count = 0;
 
    Mat FinalImage = imread("source.jpg", IMREAD_COLOR);
    if (FinalImage.empty())
        return -1;
 
    // Grayscale source and target for faster calculation.
    Mat SourceImage;
    cvtColor(FinalImage, SourceImage, CV_BGR2GRAY);
 
    Mat TargetImage = imread("target.jpg", IMREAD_GRAYSCALE);
    if (TargetImage.empty())
        return -1;
 
    Mat Result;
 
    start = clock();
    matchTemplate(SourceImage, TargetImage, Result, TM_SQDIFF_NORMED); // Type of the template matching operation: TM_SQDIFF_NORMED
    minMaxLoc(Result, &minVal, NULL&minLoc, NULL);
 
    for (int i = 0; i < Result.rows; i++)
        for (int j = 0; j < Result.cols; j++)
            if (Result.at<float>(i, j) < threshold)
            {
                rectangle(FinalImage, Point(j, i), Point(j + TargetImage.cols, i + TargetImage.rows), Scalar(00255), 1);
                count++;
            }
    end = clock();
 
    cout << "Searching time: " << difftime(end, start) / CLOCKS_PER_SEC << endl;
    cout << "Minimum Value: " << minVal << " " << minLoc << endl;
    cout << "Threshold: " << threshold << endl;
    cout << "Found: " << count << endl;
 
    imshow("TargetImage", TargetImage);
    imshow("Result", Result);
    imshow("FinalImage", FinalImage);
 
    waitKey(0);
 
    return 0;
}
cs




<Result>


Found 4 coins in 0.035 secs.




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

Template matching is a technique for finding areas of an image that match (are similar) to a template image (patch).


Python Pillow library로 구현해 봤던 Image searching 기술을 OpenCV matchTemplate 함수로 간단히 만들 수 있다.


2018/11/30 - [Software/Python] - Pillow 이미지 서치(Image Search) 1

2018/12/02 - [Software/Python] - Pillow 이미지 서치(Image Search) 2

2019/07/10 - [Software/OpenCV] - Template Matching(Image Searching) for multiple objects - 반복되는 이미지 모두 찾기

2019/07/12 - [Software/OpenCV] - Template Matching(Image Searching) with a mask for multiple objects - 마스크를 이용해 (배경이 다른) 반복되는 이미지 모두 찾기


<Target>


<Source>




Type of the template matching operation: TM_SQDIFF

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
#include <opencv2/opencv.hpp>
#include <time.h>
 
using namespace cv;
using namespace std;
 
int main()
{
    clock_t start, end;
    double minVal;
    Point minLoc;
 
    Mat FinalImage = imread("source.jpg", IMREAD_COLOR);
    if (FinalImage.empty())
        return -1;
 
    // Grayscale source and target for faster calculation.
    Mat SourceImage;
    cvtColor(FinalImage, SourceImage, CV_BGR2GRAY);
 
    Mat TargetImage = imread("target.jpg", IMREAD_GRAYSCALE);
    if (TargetImage.empty())
        return -1;
 
    Mat Result;
 
    start = clock();
    matchTemplate(SourceImage, TargetImage, Result, TM_SQDIFF); // Type of the template matching operation: TM_SQDIFF
    normalize(Result, Result, 01, NORM_MINMAX, -1, Mat());
    minMaxLoc(Result, &minVal, NULL&minLoc, NULL);
    end = clock();
 
    cout << "Searching time: " << difftime(end, start) / CLOCKS_PER_SEC << endl;
    cout << "Minimum Value: " << minVal << endl << "Location: " << minLoc << endl;
    rectangle(FinalImage, minLoc, Point(minLoc.x + TargetImage.cols, minLoc.y + TargetImage.rows), Scalar(00255), 1);
 
    imshow("TargetImage", TargetImage);
    imshow("Result", Result);
    imshow("FinalImage", FinalImage);
 
    waitKey(0);
 
    return 0;
}
cs


<Result>


Found the target at the husky's front paw in 0.014 secs.



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

Color Picker will be very useful if you are working as a Web Developer or UI designer, who mainly uses Visual Studio for the day to day assignments. This tool provides a color tool window, color swatcher in the editor.


This tool offers the below options,

  • Highlight colors in Visual Studio Document Editor (".css", ".scss", ".less", ".asp", ".aspx", ".htm", ".html", ".cshtml", ".vbhtml", ".xaml" are enabled by default), Additional Parsing, Customize using Tools-Options-ColorPicker.
  • Edit color using double click from any enabled file (editor)
  • Insert colors to any active text document
  • Color Selection from Color Canvas.
  • Color Selection from Available Colors.
  • Color Selection from Standard Colors.
  • Color Selection from Saved Colors.
  • Color Selection using Eyedropper.
  • Copy\Paste Color codes (Name, #HEX, rgb, rgba, hsl, hsla, hsb\hsv, cmyk). rgb and rgba are offered in different formats
  • Selected colors Tints & Shades.
  • Color code comparison of Orginal and New.

Visual Studio에서 원하는 색상 코드를 확인 할 수 있는 Extenstion 입니다.

색깔의 이름으로 검색하거나 코드를 색깔로, 색깔을 코드로 변환할 수도 있고 찾은 색을 저장 하거나 소스 코드에 바로 삽입하는 등의 기능이 지원 됩니다.


Tools - Extensions and Updates...


Click 'Online' tab and search 'color picker'.


Close Visual Studio.


Click 'Modify' button on VSIX installer.


It starts installing.


Click 'Close' button.


Tools - Color Picker




Click 'Color Eyedropper'. Or you can enter any name of the colors into the edit box, e.g., red, blue, green, or etc.


Move the mouse cursor and click on the color you want.


Color Picker shows the color and its codes.


You can change the format by clicking on the code format.


Click the check button and insert the color code into your source.


Tools - Options...


Color Picker - General - You can change options.



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

'Open Command Line' opens a command line at the root of the project. Support for all consoles such as CMD, PowerShell, Bash, etc. Provides syntax highlighting, Intellisense and execution of .cmd and .bat files.


This extension adds a new command to the project context menu that will open a command prompt on the project's path. If the solution node is selected in Solution Explorer, then a console will open at the root of the .sln file.


Open Command LIne은 작업 중인 Visual Studio 프로젝트 폴더에서 바로 command prompt를 열 수 있게 해주는 extension 입니다.

CMD, PowerShell, Bash등을 지원합니다.


Run Tools - Extensions and Updates...


Search 'Open Command Line' on Online tab and click Download.


Installation starts when Visual Studio closes.


Close Visual Studio and click Modify on VSIX Installer.


Install it.


Click Close.




Right-click on Solution Explorer - Open Command Line - Default(cmd) (Shortcut: Alt + Space)


Command Prompt opens on the project's path.


Right-click on Solution Explorer - Open Command Line - Settings...


You can change settings.




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

Explains how to play image sequence like a video file using OpenCV VideoCapture class.


Camera, video file등 영상을 가져오는 C++ API인 VideoCapture Class로 수백장의 image sequence를 차례로 로드해 영상처럼 재생하는 방법을 설명 합니다.


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
#include <opencv2/opencv.hpp>
 
using namespace cv;
 
int main(int argc, char** argv)
{
    VideoCapture cap("Cat\\Cat_%03d.jpg");
    // image sequence (eg. img_%02d.jpg, which will read samples like img_00.jpg, img_01.jpg, img_02.jpg, ...)
    if (!cap.isOpened())
        return -1;
 
    Mat frame;
 
    while (1)
    {
        cap.read(frame);
        if (frame.empty())
            break;
 
        imshow("Image", frame);
 
        if (waitKey(33>= 0)
            break;
    }
 
    return 0;
cs




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

cvui is a (very) simple UI lib built on top of OpenCV drawing primitives. Other UI libs, such as imgui, require a graphical backend (e.g. OpenGL) to work, so if you want to use imgui in a OpenCV app, you must make it OpenGL enabled, for instance.

It is not the case with cvui, which uses only OpenCV drawing primitives to do all the rendering (no OpenGL or Qt required).


OpenCV는 실시간 이미지 프로세싱에 중점을 둔 라이브러리로, 기본적인 GUI 지원은 상당히 빈약하기 때문에 대부분 MFC나 QT등의 라이브러리를 이용해 따로 GUI를 구현 합니다. 보통 이런 라이브러리들은 공부하고 실제 사용하는데 상당히 오랜 시간이 걸리므로 쉽고 간단히 사용할 수 있는 cvui의 사용법을 알아 보도록 하겠습니다.


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
#include <opencv2/opencv.hpp>
 
#define CVUI_IMPLEMENTATION
#include "cvui.h"
 
using namespace cv;
 
int main(int argc, char** argv)
{
    Mat img = imread("image.jpg");
    Mat canny;
    double threshold1 = 50.0;
    double threshold2 = 150.0;
    bool checked = false;
 
    if (img.empty())
        return -1;
 
    cvui::init("Canny");
 
    while (true)
    {
        if (checked)
        {
            cvtColor(img, canny, CV_BGR2GRAY);
            Canny(canny, canny, threshold1, threshold2);
            cvtColor(canny, canny, CV_GRAY2BGR);
 
            cvui::trackbar(canny, 10160195&threshold1, (double)5.0, (double)100.0);
            cvui::trackbar(canny, 10210195&threshold2, (double)50.0, (double)200.0);
        }
        else
        {
            img.copyTo(canny);
        }
 
        cvui::text(canny, 1010"cvui example"1.00xff0000);
        if (cvui::button(canny, 1060"Exit"))
            break;
        cvui::checkbox(canny, 10110"Canny edge detection"&checked);
 
        cvui::update();
 
        imshow("Canny", canny);
 
        waitKey(10);
    }
 
    return 0;
}
cs







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

Explains how to make a game(Prince of Persia) save file editor with C++.

C++를 이용한 간단한 게임 세이브 파일 에디터 소스 입니다.


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
#include <iostream>
#include <fstream>
 
using namespace std;
 
int main(int argc, char* argv[])
{
    if (argc != 3)
    {
        cout << "Usage : " << argv[0<< " [save file name] [energy]" << endl;
        cout << "Example: " << argv[0<< " prince.sav 8" << endl;
        return -1;
    }
 
    short value = atoi(argv[2]); // 2 byte value (for 16 bit old dos game)
 
    ofstream fout(argv[1], ios_base::binary | ios_base::out | ios_base::in);
    if (!fout.is_open())
    {
        cout << "Can't find " << argv[1<< endl;
        return -1;
    }
 
    fout.seekp(0x06, ios_base::beg); // Prince's energy saved at 0x06~0x07 (2 byte)
    fout.write((char*)& value, sizeof(short)); // 2 byte
 
    fout.close();
 
    cout << "Success!" << endl;
 
    return 0;
}
cs






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

Explains how to extract video from a video file using OpenCV VideoWriter.

OpenCV의 VideoWriter를 이용해 비디오 파일의 영상 부분만 추출 한다.


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()
{
    VideoCapture capture("Earth.mp4");
    CV_Assert(capture.isOpened());
    
    int fourcc = VideoWriter::fourcc('m''p''4''v');
    double fps = capture.get(CAP_PROP_FPS);
    Size size((int)capture.get(CAP_PROP_FRAME_WIDTH), (int)capture.get(CAP_PROP_FRAME_HEIGHT));
    int delay = cvRound(1000.0 / fps);
 
    cout << "FPS: " << fps << endl << "Size: " << size << endl
        << "Number of frames: " << capture.get(CAP_PROP_FRAME_COUNT) << endl;
 
    VideoWriter writer;
    writer.open("copy.mp4", fourcc, fps, size);
    CV_Assert(writer.isOpened());
 
    while (true)
    {
        Mat frame;
        capture >> frame;
        if (frame.empty())
            break;
 
        writer << frame;
 
        imshow("Earth", frame);
 
        if (waitKey(delay) >= 0)
            break;
    }
 
    return 0;
}






반응형
Posted by J-sean
: