301 lines
8.1 KiB
C++
301 lines
8.1 KiB
C++
|
#include "basic_form.h"
|
|||
|
|
|||
|
#include <atlstr.h>
|
|||
|
#include <afxdlgs.h>
|
|||
|
#include <afxwin.h>
|
|||
|
#include <io.h>
|
|||
|
#include <iosfwd>
|
|||
|
#include <iostream>
|
|||
|
#include <ios>
|
|||
|
#include <fstream>
|
|||
|
#include <vector>
|
|||
|
#include <codecvt>
|
|||
|
|
|||
|
#include "opencv2/opencv.hpp"
|
|||
|
#include "blob/blob.h"
|
|||
|
#include "blob/BlobDetector.h"
|
|||
|
#include "blob/BlobGroup.h"
|
|||
|
#include "blob/BlobContour.h"
|
|||
|
#include "i_contours_extractor.hpp"
|
|||
|
#include "contours_extractor_factory.hpp"
|
|||
|
|
|||
|
|
|||
|
|
|||
|
const std::wstring BasicForm::kClassName = L"Basic";
|
|||
|
|
|||
|
BasicForm::BasicForm()
|
|||
|
{
|
|||
|
}
|
|||
|
|
|||
|
BasicForm::~BasicForm()
|
|||
|
{
|
|||
|
}
|
|||
|
|
|||
|
std::wstring BasicForm::GetSkinFolder()
|
|||
|
{
|
|||
|
return L"basic";
|
|||
|
}
|
|||
|
|
|||
|
std::wstring BasicForm::GetSkinFile()
|
|||
|
{
|
|||
|
return L"contour.xml";
|
|||
|
}
|
|||
|
|
|||
|
std::wstring BasicForm::GetWindowClassName() const
|
|||
|
{
|
|||
|
return kClassName;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
LRESULT BasicForm::OnNcHitTest(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
|
|||
|
{
|
|||
|
return WindowImplBase::OnNcHitTest(uMsg, wParam, lParam, bHandled);
|
|||
|
}
|
|||
|
|
|||
|
inline std::string to_byte_string(const std::wstring& input)
|
|||
|
{
|
|||
|
std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
|
|||
|
return converter.to_bytes(input);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
static int bz2_read( struct bspatch_stream* stream, void* buffer, int length)
|
|||
|
{
|
|||
|
|
|||
|
return 0;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
std::vector<char>* ReadFile(std::string path) {
|
|||
|
std::vector<char>* ret = nullptr;
|
|||
|
std::ifstream file(path.c_str(), std::ios::in | std::ios::binary);
|
|||
|
if (file)
|
|||
|
{
|
|||
|
// <20><><EFBFBD>ն<EFBFBD><D5B6><EFBFBD><EFBFBD>Ƹ<EFBFBD>ʽ<EFBFBD><CABD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>
|
|||
|
file.seekg(0, std::ios::end);
|
|||
|
long long fileSize = file.tellg();
|
|||
|
printf("size of firm: %lld\n", fileSize);
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD>дλ<D0B4><CEBB><EFBFBD>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ棬<DAB4><E6A3AC><EFBFBD>̼<EFBFBD><CCBC><EFBFBD><EFBFBD>ݴ<EFBFBD><DDB4><EFBFBD>buffer
|
|||
|
file.seekg(0, std::ios::beg);
|
|||
|
char* buffer = new char[fileSize];
|
|||
|
file.read(buffer, sizeof(char) * fileSize);
|
|||
|
|
|||
|
ret = new std::vector<char>(buffer, buffer + sizeof(char) * fileSize);
|
|||
|
file.close();
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
std::cout << "Failed to open file." << std::endl;
|
|||
|
}
|
|||
|
return ret;
|
|||
|
}
|
|||
|
|
|||
|
double smin_contour = 200.0;
|
|||
|
double soffset_controu = 11;
|
|||
|
double sepsilon = 2;
|
|||
|
double ssmooth_step = 0.001;
|
|||
|
double sangel_threshold = 20.0;
|
|||
|
|
|||
|
void BasicForm::InitWindow() {
|
|||
|
|
|||
|
ui::Button* btn_open1 = dynamic_cast<ui::Button*>(FindControl(L"btn_do_open1"));
|
|||
|
ui::Button* btn_set_config = dynamic_cast<ui::Button*>(FindControl(L"btn_set_config"));
|
|||
|
|
|||
|
ui::RichEdit* min_contour = dynamic_cast<ui::RichEdit*>(FindControl(L"min_contour"));
|
|||
|
ui::RichEdit* offset_controu = dynamic_cast<ui::RichEdit*>(FindControl(L"offset_controu"));
|
|||
|
ui::RichEdit* epsilon = dynamic_cast<ui::RichEdit*>(FindControl(L"epsilon"));
|
|||
|
ui::RichEdit* angel_threshold = dynamic_cast<ui::RichEdit*>(FindControl(L"angel_threshold"));
|
|||
|
ui::RichEdit* smooth_step = dynamic_cast<ui::RichEdit*>(FindControl(L"smooth_step"));
|
|||
|
ui::RichEdit* file1_dir = dynamic_cast<ui::RichEdit*>(FindControl(L"file1_dir"));
|
|||
|
|
|||
|
ui::Control* img1 = dynamic_cast<ui::Control*>(FindControl(L"img1"));
|
|||
|
ui::Control* img2 = dynamic_cast<ui::Control*>(FindControl(L"img2"));
|
|||
|
ui::Label* cnt = dynamic_cast<ui::Label*>(FindControl(L"concnt"));
|
|||
|
|
|||
|
min_contour->SetText(L"200.0"); // 最小轮廓阀值
|
|||
|
offset_controu->SetText(L"11");
|
|||
|
epsilon->SetText(L"2");
|
|||
|
smooth_step->SetText(L"0.001");
|
|||
|
angel_threshold->SetText(L"20.0");
|
|||
|
|
|||
|
btn_set_config->AttachClick([this, min_contour, offset_controu,
|
|||
|
epsilon, smooth_step, angel_threshold,
|
|||
|
file1_dir, img1, img2, cnt](ui::EventArgs*) {
|
|||
|
size_t len = wcstombs(nullptr, min_contour->GetText().c_str(), 0) + 1;
|
|||
|
char* buffer = new char[len];
|
|||
|
wcstombs(buffer, min_contour->GetText().c_str(), len);
|
|||
|
std::string str(buffer);
|
|||
|
smin_contour = atof(str.c_str());
|
|||
|
|
|||
|
len = wcstombs(nullptr, offset_controu->GetText().c_str(), 0) + 1;
|
|||
|
buffer = new char[len];
|
|||
|
wcstombs(buffer, offset_controu->GetText().c_str(), len);
|
|||
|
std::string str1(buffer);
|
|||
|
soffset_controu = atof(str1.c_str());
|
|||
|
|
|||
|
len = wcstombs(nullptr, epsilon->GetText().c_str(), 0) + 1;
|
|||
|
buffer = new char[len];
|
|||
|
wcstombs(buffer, epsilon->GetText().c_str(), len);
|
|||
|
std::string str2(buffer);
|
|||
|
sepsilon = atof(str2.c_str());
|
|||
|
|
|||
|
|
|||
|
len = wcstombs(nullptr, smooth_step->GetText().c_str(), 0) + 1;
|
|||
|
buffer = new char[len];
|
|||
|
wcstombs(buffer, smooth_step->GetText().c_str(), len);
|
|||
|
std::string str3(buffer);
|
|||
|
ssmooth_step = atof(str3.c_str());
|
|||
|
|
|||
|
len = wcstombs(nullptr, angel_threshold->GetText().c_str(), 0) + 1;
|
|||
|
buffer = new char[len];
|
|||
|
wcstombs(buffer, angel_threshold->GetText().c_str(), len);
|
|||
|
std::string str4(buffer);
|
|||
|
sangel_threshold = atof(str4.c_str());
|
|||
|
|
|||
|
std::cout<<smin_contour<<std::endl;
|
|||
|
std::cout<<soffset_controu<<std::endl;
|
|||
|
std::cout<<sepsilon<<std::endl;
|
|||
|
std::cout<<ssmooth_step<<std::endl;
|
|||
|
std::cout<<sangel_threshold<<std::endl;
|
|||
|
|
|||
|
|
|||
|
|
|||
|
img1->SetBkImage(file1_dir->GetText());
|
|||
|
|
|||
|
len = wcstombs(nullptr, file1_dir->GetText().c_str(), 0) + 1;
|
|||
|
char* buffer1 = new char[len];
|
|||
|
wcstombs(buffer1, file1_dir->GetText().c_str(), len);
|
|||
|
std::string str5(buffer1);
|
|||
|
|
|||
|
cv::Mat color = cv::imread(str5.c_str(), cv::ImreadModes::IMREAD_UNCHANGED);
|
|||
|
|
|||
|
if (color.channels() < 3) {
|
|||
|
std::cout << "" << std::endl;
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
std::vector<cv::Mat> channels;
|
|||
|
cv::split(color, channels);
|
|||
|
|
|||
|
cv::Mat alpha = channels[3];
|
|||
|
|
|||
|
auto extractor = cvpr::ContoursExtractorFactory::create();
|
|||
|
|
|||
|
extractor->init();
|
|||
|
|
|||
|
extractor->setContourOffset(soffset_controu);
|
|||
|
|
|||
|
extractor->setApproxPolyEpsilon(sepsilon);
|
|||
|
|
|||
|
extractor->setSmoothMethod(cvpr::SMOOTH_METHOD::BSPLINE);
|
|||
|
|
|||
|
extractor->setDefectThreshold(smin_contour);
|
|||
|
|
|||
|
extractor->setSmoothStep(ssmooth_step);
|
|||
|
|
|||
|
std::vector<std::vector<cv::Point>> contours;
|
|||
|
try
|
|||
|
{
|
|||
|
extractor->extract(alpha, contours);
|
|||
|
|
|||
|
}
|
|||
|
catch (const std::exception& e)
|
|||
|
{
|
|||
|
std::cout << e.what() << std::endl;
|
|||
|
}
|
|||
|
cv::Mat img = color.clone();
|
|||
|
//cv::namedWindow("camera", 0);//CV_WINDOW_NORMAL就是0
|
|||
|
img = img.clone();
|
|||
|
cv::Mat empty_mask = cv::Mat::ones(img.rows, img.cols, CV_8UC3) * 255;
|
|||
|
cv::drawContours(img, contours, -1, cv::Scalar(0, 0, 255, 255), 3);
|
|||
|
|
|||
|
cv::imwrite("d://res2222.png", img);
|
|||
|
img2->SetBkImage(L"d://res2222.png");
|
|||
|
wchar_t t[200];
|
|||
|
wsprintf(t, L"count %d", contours[0].size());
|
|||
|
cnt->SetText(std::wstring(t));
|
|||
|
extractor->destroy();
|
|||
|
return true;
|
|||
|
});
|
|||
|
|
|||
|
btn_open1->AttachClick([this, file1_dir, img1, img2, cnt](ui::EventArgs*) {
|
|||
|
CString strFilter;
|
|||
|
CString m_strTmpFile;
|
|||
|
CStdioFile cfLogFile;
|
|||
|
|
|||
|
strFilter = "dat file (*.*)|*.*";
|
|||
|
CFileDialog TmpDlg(true, 0, 0, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, strFilter.GetBuffer(),
|
|||
|
CWnd::FromHandle(this->m_hWnd));
|
|||
|
|
|||
|
if (TmpDlg.DoModal() == IDOK)
|
|||
|
{
|
|||
|
m_strTmpFile = TmpDlg.GetPathName();
|
|||
|
|
|||
|
file1_dir->SetText(std::wstring(m_strTmpFile));
|
|||
|
}
|
|||
|
img1->SetBkImage(file1_dir->GetText());
|
|||
|
|
|||
|
size_t len = wcstombs(nullptr, file1_dir->GetText().c_str(), 0) + 1;
|
|||
|
char* buffer = new char[len];
|
|||
|
wcstombs(buffer, file1_dir->GetText().c_str(), len);
|
|||
|
std::string str2(buffer);
|
|||
|
|
|||
|
cv::Mat color = cv::imread(str2.c_str(), cv::ImreadModes::IMREAD_UNCHANGED);
|
|||
|
|
|||
|
if (color.channels() < 3) {
|
|||
|
std::cout << "" << std::endl;
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
std::vector<cv::Mat> channels;
|
|||
|
cv::split(color, channels);
|
|||
|
|
|||
|
cv::Mat alpha = channels[3];
|
|||
|
|
|||
|
auto extractor = cvpr::ContoursExtractorFactory::create();
|
|||
|
|
|||
|
extractor->init();
|
|||
|
|
|||
|
extractor->setContourOffset(soffset_controu);
|
|||
|
|
|||
|
extractor->setApproxPolyEpsilon(sepsilon);
|
|||
|
|
|||
|
extractor->setSmoothMethod(cvpr::SMOOTH_METHOD::BSPLINE);
|
|||
|
|
|||
|
extractor->setDefectThreshold(smin_contour);
|
|||
|
|
|||
|
extractor->setSmoothStep(ssmooth_step);
|
|||
|
|
|||
|
std::vector<std::vector<cv::Point>> contours;
|
|||
|
try
|
|||
|
{
|
|||
|
extractor->extract(alpha, contours);
|
|||
|
|
|||
|
}
|
|||
|
catch (const std::exception& e)
|
|||
|
{
|
|||
|
std::cout << e.what()<<std::endl;
|
|||
|
}
|
|||
|
cv::Mat img = color.clone();
|
|||
|
//cv::namedWindow("camera", 0);//CV_WINDOW_NORMAL就是0
|
|||
|
img = img.clone();
|
|||
|
cv::Mat empty_mask = cv::Mat::ones(img.rows, img.cols, CV_8UC3) * 255;
|
|||
|
cv::drawContours(img, contours, -1, cv::Scalar(0, 0, 255,255), 3);
|
|||
|
|
|||
|
cv::imwrite("d://res2222.png", img);
|
|||
|
img2->SetBkImage(L"d://res2222.png");
|
|||
|
wchar_t t[200];
|
|||
|
|
|||
|
wsprintf(t, L"cnt: %d", contours[0].size());
|
|||
|
cnt->SetText(L"shit");
|
|||
|
extractor->destroy();
|
|||
|
return true;
|
|||
|
});
|
|||
|
}
|
|||
|
|
|||
|
LRESULT BasicForm::OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
|
|||
|
{
|
|||
|
PostQuitMessage(0L);
|
|||
|
return __super::OnClose(uMsg, wParam, lParam, bHandled);
|
|||
|
}
|