반응형

이미지 파일을 어떻게 불러오는지에 따라 해상도가 다르게 적용될 수 있다.

 

Reindeer.png
0.08MB

 

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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Image i = Image.FromFile("Reindeer.png");
            Bitmap b1 = (Bitmap)Bitmap.FromFile("Reindeer.png");
            Bitmap b2 = new Bitmap(Image.FromFile("Reindeer.png"));
            
            // 100 pixel 간격
            e.Graphics.DrawImage(i, 00);
            e.Graphics.DrawImage(b1, 1000);
            e.Graphics.DrawImage(b2, 2000);
 
            // 해상도 출력
            string str = string.Format("i: {0}X{1}, b1: {2}X{3}, b2: {4}X{5}", i.HorizontalResolution, i.VerticalResolution,
                b1.HorizontalResolution, b1.VerticalResolution, b2.HorizontalResolution, b2.VerticalResolution);
            e.Graphics.DrawString(str, Font, Brushes.Black ,100510);
        }
    }
}
 

 

같은 파일(Reindeer.png)을 세 가지 방법으로 불러와서 100 픽셀 간격으로 출력해 보자.

 

i, b1과 b2의 해상도가 다르다.

 

예상한 것과 다른 방식으로 출력된다. 다른 이미지 뷰어에 표시되는 사이즈와 동일한 방식은 b2다. i, b1은 약간 확대(96/72=1.3배) 되었다. 이미지 파일을 사용할 때는 어떤식으로 불러올지 잘 선택하자.

 

파일에 따라 어떻게 불러와도 같은 사이즈로 표시되기도 한다.

 

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

바탕화면에 이미지를 출력하고 움직여 보자.

 

폼과 버튼을 적당히 배치한다.

 

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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
using System.Runtime.InteropServices;
 
namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
        private static extern IntPtr GetDC(IntPtr hWnd);
 
        [DllImport("user32.dll", ExactSpelling = true)]
        private static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);
 
        public Form1()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            int step = 1;   // 이동 스텝
 
            Rectangle targetRect = new Rectangle(100150400400);   // 표시할 그림 사이즈(Horizontal: 100~499(400) pixel, Vertical: 150~549(400) pixel) 
            Point outPoint = new Point(500300);   // 모니터의 표시 위치(x, y)
 
            //Image img = Image.FromFile("Barbara.jpg");  // Resolution Horizontal: 300, Vertical: 300  
            // 이렇게 하면 이미지 파일과 아래 buffer 비트맵의 해상도(96, 96)가 달라서 (사이즈가) 제대로 표시되지 않는다.
            Bitmap img = new Bitmap(Image.FromFile("Barbara.jpg")); // Resolution Horizontal: 96, Vertical: 96
            Bitmap scr = ScreenCapture.Capture();
            Bitmap buffer = new Bitmap(targetRect.Width + step, targetRect.Height + step);
            IntPtr desktopDC = GetDC(IntPtr.Zero);
 
            Graphics dG = Graphics.FromHdc(desktopDC);
            Graphics bG = Graphics.FromImage(buffer);
            
            for (int i = 0, j = 0; i < 100; i += step, j += step)
            {
                bG.DrawImage(scr, 00new Rectangle(outPoint.X + i, outPoint.Y + j, targetRect.Width + step , targetRect.Height + step ), GraphicsUnit.Pixel);
                bG.DrawImage(img, step, step, targetRect, GraphicsUnit.Pixel);                
                dG.DrawImage(buffer, outPoint.X + i, outPoint.Y + j);
            }
 
            for (int i = 100, j = 100; i > 0; i -= step, j -= step)
            {
                bG.DrawImage(scr, 00new Rectangle(outPoint.X + i, outPoint.Y + j, targetRect.Width + step, targetRect.Height + step), GraphicsUnit.Pixel);
                bG.DrawImage(img, 00, targetRect, GraphicsUnit.Pixel);
                dG.DrawImage(buffer, outPoint.X + i, outPoint.Y + j);
            }
 
            dG.Dispose();
            bG.Dispose();
            ReleaseDC(IntPtr.Zero, desktopDC);
        }
    }
 
    public class ScreenCapture
    {
        [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
        private static extern IntPtr GetDC(IntPtr hWnd);
 
        [DllImport("user32.dll", ExactSpelling = true)]
        private static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);
 
        [DllImport("gdi32.dll", ExactSpelling = true)]
        private static extern IntPtr BitBlt(IntPtr hDestDC, int x, int y, int nWidth, int nHeight, IntPtr hSrcDC, int xSrc, int ySrc, int dwRop);
 
        [DllImport("user32.dll", EntryPoint = "GetDesktopWindow")]
        private static extern IntPtr GetDesktopWindow();
 
        public static Bitmap Capture()
        {
            int screenWidth = Screen.PrimaryScreen.Bounds.Width;
            int screenHeight = Screen.PrimaryScreen.Bounds.Height;
 
            Bitmap screenBmp = new Bitmap(screenWidth, screenHeight);
            Graphics g = Graphics.FromImage(screenBmp);
 
            IntPtr desktopDC = GetDC(GetDesktopWindow());
            IntPtr hDC = g.GetHdc();
 
            BitBlt(hDC, 00, screenWidth, screenHeight, desktopDC, 000x00CC0020);    //SRCCOPY (DWORD)0x00CC0020
 
            ReleaseDC(GetDesktopWindow(), desktopDC);
            g.ReleaseHdc(hDC);
            g.Dispose();
 
            return screenBmp;
        }
    }
}
 

 

소스를 입력하고 빌드한다.

Barbara.jpg
0.05MB

 

실행하고 버튼을 클릭한다.

 

지정된 위치에 이미지가 출력되고 움직인다.

 

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

KiCad에는 PCB에 로고(그림)를 넣는 메뉴가 없다. 직선과 원만으로 로고를 그리기는 어려우므로 아래 방법을 사용한다.

 

KiCad를 실행하고 새로운 프로젝트를 만든다.

 

Bitmap to Component Converter를 실행한다.

 

Load Bitmap 버튼을 클릭하고 원하는 로고나 사진을 불러온다.

 

Format은 Pcbnew(.kicad_mod file), Board Layer for Outline은 Front silk screen으로 선택한다.

 

Greyscale Picture

 

 

Black&White Picture

PCB위에 silk screen으로 적용하는 경우 결과는 Black&White Picture와 거의 같다. Picture가 마음에 들지 않는다면 원본 파일을 적당히 전처리 해 준다. 이미지가 너무 크거나 작다면 미리 사이즈를 수정해야 할 수도 있다. 상단의 Bitmap Info와 Image Options의 Black/White Threshold를 확인하자.

 

이미지 선택이 완료 되었으면 Export를 클릭하고 적당한 폴더에 Pcbnew 파일을 만든다.

 

생성된 Pcbnew(.kicad_mod) 파일은 이미지 정보가 들어있는 텍스트 파일이다.

 

Bitmap to Component Converter를 종료하고 Footprint Editor를 클릭한다.

 

Footprint Editor

 

File - New Library...를 클릭한다.

 

 

적당한 폴더에 라이브러리 경로 파일(.pretty)을 저장한다.

 

이 프로젝트에서만 사용할 Footprint 라이브러리라면 Project를 선택한다.

 

Test 라이브러리가 만들어졌다.

 

File - Import Footprint from KiCad File...을 클릭하고 Bitmap to Component Converter에서 만든 Pcbnew 파일을 선택한다.

 

이미지가 import된다.

 

 

File - Save를 클릭하고 Test 라이브러리에 저장한다.

 

Footprint Editor를 종료하고 PCB Layout Editor를 클릭한다.

 

Add footprints를 클릭한다.

 

Footprint Editor에서 저장한 Test - camping을 선택한다.

 

이미지가 silk screen으로 표시된다.

 

 

이렇게 완성된다.

 

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

리눅스(우분투)에서 GTK+를 이용해 이미지를 디스플레이 해 보자.

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
#include <gtk/gtk.h>
 
static gboolean button_press_callback(GtkWidget* event_box, GdkEventButton* event, gpointer data)
{
    g_print("Event box clicked at coordinates %f,%f\n", event->x, event->y);
 
    return TRUE;
}
 
static void activate(GtkApplication* app, gpointer user_data)
{
    GtkWidget* window;
    GtkWidget* image;
    GtkWidget* event_box;
 
    window = gtk_application_window_new(app);
    gtk_window_set_title(GTK_WINDOW(window), "Window");
    gtk_window_set_default_size(GTK_WINDOW(window), 300300);
 
    event_box = gtk_event_box_new();
    gtk_container_add(GTK_CONTAINER(window), event_box);
 
    image = gtk_image_new_from_file("Barbara Palvin.jpg");
    gtk_container_add(GTK_CONTAINER(event_box), image);
 
    g_signal_connect(G_OBJECT(event_box), "button_press_event", G_CALLBACK(button_press_callback), image);
 
    gtk_widget_show_all(window);
}
 
int main(int argc, char** argv)
{
    GtkApplication* app;
    int status;
 
    app = gtk_application_new("sean.s-engineer.example", G_APPLICATION_FLAGS_NONE);
    g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
    status = g_application_run(G_APPLICATION(app), argc, argv);
    g_object_unref(app);
 
    return status;
}
 
 
 

 

컴파일(빌드)하고 실행하면 창에 이미지가 디스플레이 된다. 클릭하면 좌표가 터미널에 표시된다.

 

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

레트로파이에 한글 폰트, 게임 마키, 이미지, 플레이 영상, 설명등을 넣어 보자.

 

2020/02/13 - [Raspberry Pi & Arduino] - Raspberry Pi Desktop(Debian), Ubuntu에서 RetroPie 설치하기

 

특별한 설정을 하지 않았다면 레트로파이(아케이드)에 게임 이름 정도만 표시된다. 등록된 게임을 잘 아는 사람은 문제 없겠지만 모르는 사람은 무슨 게임인지 알 수가 없다. 처음 하는 사람도 어떤 게임인지 알 수 있도록 이미지, 플레이 영상, 설명등을 넣어 보자.

 

Cabin-Bold.ttf 가 기본(영문) 폰트다. 한글이 표시 될 수 있도록 원하는 한글 폰트(KATURI.TTF)를 /etc/emulationstation/themes/carbon/art/ 에 복사한다. 

 

/etc/emulationstation/themes/carbon/ 에 있는 'carbon.xml', 'theme.xml' 파일에 설정된 폰트를 변경한다.

 

두 파일을 열고 'Cabin-Bold.ttf'가 나오는 부분을 모두 한글 폰트 이름(KATURI.TTF)으로 바꾼다. (원래 있던 'Cabin-Bold.ttf' 파일을 지우고 한글 폰트 이름을 'Cabin-Bold.ttf'로 바꾸면 두 파일의 내용을 바꾸지 않아도 된다)

 

 

RetroPie를 실행해 보면 바뀐 폰트가 적용되어 있다.

 

/opt/retropie/configs/all/emulationstation/gamelists/arcade/gamelist.xml 에 게임 이름, 이미지, 플레이 영상, 설명등을 등록 할 수 있다.

 

gamelist.xml
5.73MB

 

아래와 같은 형식으로 작성 한다.

 

<?xml version="1.0"?>

<gameList>

<game>

<path>게임 파일 경로</path>

<name>게임 이름</name>

<image>게임 이미지 파일 경로</image>

<video>게임 플레이 영상 파일 경로</video>

<marquee>게임 마키 파일 경로</marquee>

<developer>게임 제작사</developer>

<publisher>게임 판매사</publisher>

<genre>게임 장르</genre>

<players>게임 플레이어 수</players>

<releasedate>게임 발매일</releasedate>

<desc>게임 설명</desc>

</game>

<game>

...

</game>

...

</gameList>

 

'gamelist.xml'에 등록한 파일 경로 및 이름대로 marquee, snap, video 디렉토리를 /home/pi/RetroPie/roms/arcade/ 에 생성하고 각 디렉토리에 맞는 파일을 복사한다.

 

 

마키(marquee)

 

이미지(image)/스냅(snap)

 

영상(video)

 

 

예를 들어 marquee 디렉토리의 내용은 위와 같다.

 

RetroPie를 실행해 보자. ARCADE - 메탈 슬러그의 마키, 플레이 영상, 게임 설명등이 디스플레이 된다.

 

스트리트 파이터 2의 내용도 잘 디스플레이 된다.

 

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

2021.01.04 - [Linux] - Open Image Files From Terminal in Linux(Ubuntu) - 리눅스(우분투) 터미널에서 이미지 파일 열기

 

리눅스(우분투) 서버 같은 콘솔 환경에서도 이미지 뷰어를 사용할 수 있다.

 

fbi를 설치한다. fbi는 리눅스의 framebuffer를 이용해 이미지를 디스플레이한다. 

 

sudo fbi '이미지 파일' 형식으로 실행한다.

 

이미지가 디스플레이 된다.

 

h키를 누르면 keyboard commands를 볼 수 있다.

 

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

보통 이미지 뷰어는 투명 영역을 체크무늬나 검정색으로 표시한다. 이미지의 투명한 영역이 투과되어 보이는 이미지 뷰어를 사용해 보자.


투명 배경에 흰색 글자가 표시되어 있다. 흰색 배경의 브라우저를 사용한다면 위 이미지가 보이지 않는다.


pqiv를 설치한다.


pqiv -c -i -P '50, 50' sean.png 명령을 실행한다. '&' 옵션을 주지 않아도 백그라운드로 실행된다.


-c, --transparent-background

Draw pqiv's window borderless and transparent. In window mode, a mouse click activates and deactivates window decorations.


-i, --hide-info-box

Initially hide the infobox. Whether the box is visible can be toggled by pressing 'i' at runtime by default.


-P, --window-position=POSITION

Set the initial window position. POSITION may either be 'x,y'  screen coordinates or 'off' to not position the window at all.


그 외 옵션은 Ubuntu Manpage를 참고하자.


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

Only two matching methods currently accept a mask: CV_TM_SQDIFF and CV_TM_CCORR_NORMED


The mask should have a CV_8U or CV_32F depth and the same number of channels and size as the target image. In CV_8U case, the mask values are treated as binary, i.e. zero and non-zero. In CV_32F case, the values should fall into [0..1] range and the target image pixels will be multiplied by the corresponding mask pixel values.


OpenCV matchTemplate 함수에 마스크를 적용해서 (배경이 다른) 같은 이미지를 모두 찾을 수 있다. 마스크는 CV_8U 아니면 CV_32F의 깊이값을 가져야 하며 target image와 같은 채널 수와 사이즈를 가져야 한다.


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

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


<Target>


<Mask>


<Source>


There are 3 objects(bones) to find in the source image.

Each of them has a different background as below.


Below code explains how to spot different background multiple objects with a mask.

Adjust threshold value if it doesn't work properly.

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
#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.001;
    int count = 0;
 
    Mat FinalImage = imread("source.png", IMREAD_COLOR);
    if (FinalImage.empty())
        return -1;
 
    // Grayscale source, target and mask for faster calculation.
    Mat SourceImage;
    cvtColor(FinalImage, SourceImage, CV_BGR2GRAY);
 
    Mat TargetImage = imread("target.png", IMREAD_GRAYSCALE);
    if (TargetImage.empty())
        return -1;
 
    Mat Mask = imread("mask.png", IMREAD_GRAYSCALE);
    if (Mask.empty())
        return -1;
 
    Mat Result;
 
    start = clock();
    // Mask must have the same datatype and size with target image.
    // It is not set by default. Currently, only the TM_SQDIFF and TM_CCORR_NORMED methods are supported.
    matchTemplate(SourceImage, TargetImage, Result, TM_SQDIFF, Mask); // Type of the template matching operation: TM_SQDIFF
    normalize(Result, Result, 01, NORM_MINMAX, -1, Mat());
    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("Mask", Mask);
    imshow("TargetImage", TargetImage);
    imshow("Result", Result);
    imshow("FinalImage", FinalImage);
 
    waitKey(0);
 
    return 0;
}
cs




Grayscale target image


Binary mask


Result image


Final image


Found 3 bones in 0.097 secs.



반응형
Posted by J-sean
: