base:基本图像显示。

This commit is contained in:
taynpg 2024-12-26 15:55:16 +08:00
parent 9caf79b3f4
commit cd4f293551
6 changed files with 116 additions and 1 deletions

View File

@ -31,5 +31,9 @@
"ja": true, "ja": true,
"zh-hant": true, "zh-hant": true,
"zh-hans": true "zh-hans": true
},
"files.associations": {
"any": "cpp",
"filesystem": "cpp"
} }
} }

View File

@ -17,6 +17,6 @@ add_subdirectory(ofen)
find_package(wxWidgets CONFIG REQUIRED) find_package(wxWidgets CONFIG REQUIRED)
find_package(OpenCV CONFIG REQUIRED) find_package(OpenCV CONFIG REQUIRED)
add_executable(PictureTool main.cpp MainFrame.h MainFrame.cpp) add_executable(PictureTool main.cpp MainFrame.h MainFrame.cpp WinImg.h WinImg.cpp)
target_link_libraries(PictureTool PRIVATE wx::core wx::base Ofen ${OpenCV_LIBS}) target_link_libraries(PictureTool PRIVATE wx::core wx::base Ofen ${OpenCV_LIBS})
set_target_properties(PictureTool PROPERTIES WIN32_EXECUTABLE TRUE) set_target_properties(PictureTool PROPERTIES WIN32_EXECUTABLE TRUE)

View File

@ -1,9 +1,63 @@
#include "MainFrame.h" #include "MainFrame.h"
constexpr auto g_Border = 2;
CMainFrame::CMainFrame(const wxString& title) : wxFrame(NULL, wxID_ANY, title) CMainFrame::CMainFrame(const wxString& title) : wxFrame(NULL, wxID_ANY, title)
{ {
SetSize(wxSize(600, 300));
init();
} }
CMainFrame::~CMainFrame() CMainFrame::~CMainFrame()
{ {
} }
void CMainFrame::init()
{
auto* panel = new wxPanel(this);
auto* sizerTop = new wxBoxSizer(wxVERTICAL);
auto* sizeA = new wxBoxSizer(wxHORIZONTAL);
auto* pic_path = new wxTextCtrl(panel, wxID_ANY);
auto* select_pic = new wxButton(panel, wxID_ANY, wxT("选择"));
sizeA->Add(pic_path, 1, wxALL | wxEXPAND, g_Border);
sizeA->Add(select_pic, 0, wxALL, g_Border);
sizerTop->Add(sizeA, 0, wxALL | wxEXPAND, g_Border);
win_img_ = new CImagePanel(panel);
sizerTop->Add(win_img_, 1, wxALL | wxEXPAND, g_Border);
img_ = cv::imread("D:/2.png");
win_img_->set_img(img_);
auto* sizeB = new wxBoxSizer(wxHORIZONTAL);
auto* btn2Blue = new wxButton(panel, wxID_ANY, wxT("蓝底"));
auto* btn2White = new wxButton(panel, wxID_ANY, wxT("白底"));
sizeB->Add(btn2Blue, 1, wxALL | wxEXPAND, g_Border);
sizeB->Add(btn2White, 1, wxALL | wxEXPAND, g_Border);
btn2Blue->Bind(wxEVT_BUTTON, &CMainFrame::turn2blue, this);
btn2White->Bind(wxEVT_BUTTON, &CMainFrame::turn2white, this);
sizerTop->Add(sizeB, 0, wxALL | wxEXPAND, g_Border);
panel->SetSizer(sizerTop);
panel->Layout();
}
void CMainFrame::turn2blue(wxCommandEvent& event)
{
// 定义白色的阈值范围
cv::Scalar lowerWhite(200, 200, 200); // 白色下限 (B, G, R)
cv::Scalar upperWhite(255, 255, 255); // 白色上限 (B, G, R)
// 创建蓝色背景
cv::Mat mask;
cv::inRange(img_, lowerWhite, upperWhite, mask); // 检测白色区域
// 将白色区域替换为蓝色
cv::Mat blueBackground(img_.size(), img_.type(), cv::Scalar(255, 0, 0)); // 蓝色 (B, G, R)
blueBackground.copyTo(img_, mask); // 通过掩码复制蓝色区域到原图
win_img_->set_img(img_);
}
void CMainFrame::turn2white(wxCommandEvent& event)
{
}

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <wx/wx.h> #include <wx/wx.h>
#include "WinImg.h"
class CMainFrame : public wxFrame class CMainFrame : public wxFrame
{ {
@ -7,5 +8,13 @@ public:
CMainFrame(const wxString& title); CMainFrame(const wxString& title);
virtual ~CMainFrame(); virtual ~CMainFrame();
public: public:
void init();
private:
void turn2blue(wxCommandEvent& event);
void turn2white(wxCommandEvent& event);
private:
cv::Mat img_;
CImagePanel* win_img_;
}; };

29
WinImg.cpp Normal file
View File

@ -0,0 +1,29 @@
#include "WinImg.h"
CImagePanel::CImagePanel(wxWindow* parent)
: wxPanel(parent), bitmap_(wxNullBitmap)
{
Bind(wxEVT_PAINT, &CImagePanel::OnPaint, this);
}
void CImagePanel::set_img(const cv::Mat& img)
{
if (img.empty()) {
return;
}
cv::Mat rgb_img;
cv::cvtColor(img, rgb_img, cv::COLOR_BGR2RGB);
wxImage wimg(rgb_img.cols, rgb_img.rows, rgb_img.data, true);
bitmap_ = wxBitmap(wimg);
Refresh();
}
void CImagePanel::OnPaint(wxPaintEvent& event)
{
wxPaintDC dc(this);
if (bitmap_.IsOk()) {
dc.DrawBitmap(bitmap_, 0, 0, false);
}
}

19
WinImg.h Normal file
View File

@ -0,0 +1,19 @@
#pragma once
#include <wx/wx.h>
#include <opencv2/opencv.hpp>
class CImagePanel : public wxPanel
{
public:
CImagePanel(wxWindow* parent);
public:
void set_img(const cv::Mat& img);
protected:
void OnPaint(wxPaintEvent& event);
private:
wxBitmap bitmap_;
};