3536 lines
147 KiB
C++
3536 lines
147 KiB
C++
#include "Calibration.h"
|
||
#include <vector>
|
||
#include <opencv2/features2d.hpp>
|
||
#include <opencv2\calib3d.hpp>
|
||
#include <opencv2\imgproc\types_c.h>
|
||
#include <opencv2/highgui.hpp>
|
||
|
||
#include <opencv2/opencv_modules.hpp>
|
||
#include "../config/ConfigManager.h"
|
||
#include <fstream>
|
||
#include <gl/GL.h>
|
||
#include <GL/khronos_glext.h>
|
||
#include "../external/imgui/imgui_custom.h"
|
||
#include "../Logger.h"
|
||
#include "../utils/MathHelper.h"
|
||
#include "../Toast.h"
|
||
#include "../global.h"
|
||
#include <io.h>
|
||
#include <direct.h>
|
||
|
||
CalibrationShowInfo::~CalibrationShowInfo()
|
||
{
|
||
if (m_ShowTex > 0) {
|
||
glDeleteTextures(1, &m_ShowTex);
|
||
m_ShowTex = 0;
|
||
}
|
||
m_ImageMat.release();
|
||
}
|
||
|
||
void CalibrationShowInfo::Init(cv::Mat image, unsigned char* imageData)
|
||
{
|
||
m_ImageMat = image;
|
||
m_ImageData = imageData;
|
||
glGenTextures(1, &m_ShowTex);
|
||
m_Width = m_ImageMat.cols;
|
||
m_High = m_ImageMat.rows;
|
||
m_PixelRatio = (float)m_Width / m_High;
|
||
if (m_ShowTex > 0) {
|
||
glBindTexture(GL_TEXTURE_2D, m_ShowTex);
|
||
|
||
if (m_IsColor) {
|
||
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
|
||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, m_Width, m_High, 0, GL_RGB, GL_UNSIGNED_BYTE, m_ImageMat.data);
|
||
}
|
||
else {
|
||
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
||
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, m_Width, m_High, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, m_ImageMat.data);
|
||
}
|
||
//glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
|
||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||
glBindTexture(GL_TEXTURE_2D, 0);
|
||
}
|
||
|
||
}
|
||
|
||
void CalibrationShowInfo::Init()
|
||
{
|
||
if (m_ShowTex > 0) {
|
||
glDeleteTextures(1, &m_ShowTex);
|
||
m_ShowTex = 0;
|
||
}
|
||
if (m_ShowTex == 0) {
|
||
glGenTextures(1, &m_ShowTex);
|
||
m_Width = m_ImageMat.cols;
|
||
m_High = m_ImageMat.rows;
|
||
m_PixelRatio = (float)m_Width / m_High;
|
||
glBindTexture(GL_TEXTURE_2D, m_ShowTex);
|
||
if (m_IsColor) {
|
||
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
|
||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, m_Width, m_High, 0, GL_RGB, GL_UNSIGNED_BYTE, m_ImageMat.data);
|
||
}
|
||
else {
|
||
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
||
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, m_Width, m_High, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, m_ImageMat.data);
|
||
}
|
||
//glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, m_Width, m_High, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, m_ImageMat.data);
|
||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||
glBindTexture(GL_TEXTURE_2D, 0);
|
||
}
|
||
}
|
||
|
||
void CalibrationShowInfo::Update()
|
||
{
|
||
if (m_ShowTex > 0) {
|
||
glBindTexture(GL_TEXTURE_2D, m_ShowTex);
|
||
if (m_IsColor) {
|
||
//glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
|
||
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, m_Width, m_High, GL_RGB, GL_UNSIGNED_BYTE, m_ImageMat.data);
|
||
}
|
||
else {
|
||
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
||
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, m_Width, m_High, GL_LUMINANCE, GL_UNSIGNED_BYTE, m_ImageMat.data);
|
||
}
|
||
//glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, m_Width, m_High, GL_LUMINANCE, GL_UNSIGNED_BYTE, m_ImageMat.data);
|
||
glBindTexture(GL_TEXTURE_2D, 0);
|
||
}
|
||
}
|
||
|
||
Calibration::Calibration(JobController* jc, HBDCamera* camera)
|
||
{
|
||
InitializeCriticalSection(&cab_cs);
|
||
//m_CalibrationSrc = cv::Mat(cv::Size(800, 800), CV_8U);
|
||
// m_CalibrationShow = cv::Mat(cv::Size(800, 800), CV_8U);
|
||
//isDispUpdated = false;
|
||
m_ExtCfg = ConfigManager::GetInstance()->GetExtCfg();
|
||
m_CameraCalibrationCfg = ConfigManager::GetInstance()->GetCameraCalibrationCfg();
|
||
m_GraftingShow = false;
|
||
m_Jc = jc;
|
||
m_Camera = camera;
|
||
m_CalShowSelectIndex = 0;
|
||
m_CalibrationErrorInfo = "";
|
||
m_LocationImage = NULL;
|
||
m_CoverImage = NULL;
|
||
m_BasePartImage = NULL;
|
||
m_LastMouRefImgPosX = 0;
|
||
m_LastMouRefImgPosY = 0;
|
||
m_GraftThread = INVALID_HANDLE_VALUE;
|
||
m_GraftFlag = false;
|
||
m_CalibrationCameraThread = INVALID_HANDLE_VALUE;
|
||
m_CalibrationCameraFlag = false;
|
||
m_CalibrationCameraProgress = 0.0f;
|
||
m_GraftInfo = "";
|
||
m_GraftProgress = 0.0f;
|
||
InitializeCriticalSection(&m_CalibrationCameraCS);
|
||
InitializeCriticalSection(&m_GraftCS);
|
||
m_ImgPath = g_AppPath + "calibration/";
|
||
if (_access(m_ImgPath.c_str(), 0) != 0) {
|
||
_mkdir(m_ImgPath.c_str());
|
||
}
|
||
m_CoverImgPath = m_ImgPath + "cover.bmp";
|
||
|
||
//m_Light = ConfigManager::GetInstance()->GetIoCfgWrapper()->m_CameraLight;
|
||
m_Door = ConfigManager::GetInstance()->GetIoCfgWrapper()->m_CarbinDoorClose;
|
||
m_GraftShowImage = new CalibrationShowInfo();
|
||
}
|
||
|
||
void Calibration::Init()
|
||
{
|
||
cv::Mat coverMat = cv::imread(m_CoverImgPath);
|
||
if (coverMat.data != NULL)
|
||
{
|
||
CalibrationShowInfo* info = new CalibrationShowInfo();
|
||
cv::cvtColor(coverMat, coverMat, CV_RGBA2GRAY);
|
||
info->Init(coverMat, coverMat.data);
|
||
m_CoverImage = info;
|
||
}
|
||
|
||
m_ColorMap.push_back(cv::Scalar(30, 105, 210));
|
||
m_ColorMap.push_back(cv::Scalar(10, 215, 255));
|
||
m_ColorMap.push_back(cv::Scalar(0, 255, 255));
|
||
m_ColorMap.push_back(cv::Scalar(0, 128, 128));
|
||
m_ColorMap.push_back(cv::Scalar(230, 216, 173));
|
||
m_ColorMap.push_back(cv::Scalar(203, 192, 255));
|
||
m_ColorMap.push_back(cv::Scalar(238, 130, 238));
|
||
m_ColorMap.push_back(cv::Scalar(240, 230, 140));
|
||
m_ColorMap.push_back(cv::Scalar(61, 145, 64));
|
||
m_ColorMap.push_back(cv::Scalar(65, 105, 225));
|
||
}
|
||
|
||
Calibration::~Calibration()
|
||
{
|
||
DeleteCriticalSection(&m_CalibrationCameraCS);
|
||
DeleteCriticalSection(&cab_cs);
|
||
}
|
||
|
||
void Calibration::UpdateData(MetaData* metaData) {
|
||
EnterCriticalSection(&cab_cs);
|
||
m_MetaData = metaData;
|
||
m_MetaData->LoadFirstLayer();
|
||
LeaveCriticalSection(&cab_cs);
|
||
if (m_GraftShowImage->m_ImageMat.data != NULL && m_GraftMat.data != NULL) {
|
||
EnterCriticalSection(&m_Jc->m_cs);
|
||
m_ManualGraftSelectPart = 0;
|
||
m_GraftShowImage->m_ImageMat.release();
|
||
m_GraftMat.copyTo(m_GraftShowImage->m_ImageMat);
|
||
cv::cvtColor(m_GraftShowImage->m_ImageMat, m_GraftShowImage->m_ImageMat, cv::COLOR_GRAY2RGB);
|
||
m_GraftShowImage->m_IsColor = true;
|
||
if (m_Jc->GetJob()) {
|
||
int xLength = (m_CameraCalibrationCfg->m_PlatformTopRightX->GetValue() - m_CameraCalibrationCfg->m_PlatformTopLeftX->GetValue())*m_CameraCalibrationCfg->m_MagnifyScale->GetValue();
|
||
int yLength = (m_CameraCalibrationCfg->m_PlatformTopRightY->GetValue() - m_CameraCalibrationCfg->m_PlatformBottomRightY->GetValue())*m_CameraCalibrationCfg->m_MagnifyScale->GetValue();
|
||
int centerx = xLength / 2;
|
||
int centery = yLength / 2;
|
||
FileProcessor *jfp = m_Jc->GetJob();
|
||
MetaData::Layer* player = jfp->GetLayer(0);
|
||
|
||
|
||
for (unsigned int dbIndex = 0; dbIndex < player->data_blocks.size(); ++dbIndex) {
|
||
MetaData::DataBlock* db = player->data_blocks[dbIndex];
|
||
if (jfp->GetMetaData()->m_FirstLayerBlockMap.find(db) == jfp->GetMetaData()->m_FirstLayerBlockMap.end())continue;
|
||
BPBinary::BinDataBlock* pdb = jfp->GetMetaData()->m_FirstLayerBlockMap[db];
|
||
if (!pdb)continue;
|
||
if (pdb->type != BIN_CHAIN)continue;
|
||
|
||
MetaData::Part* cpart = jfp->GetMetaData()->GetPart(db->references->part);
|
||
|
||
for (unsigned int j = 0; j < pdb->point_indexs.size(); ++j) {
|
||
BPBinary::ChainPoint* point = (BPBinary::ChainPoint*)pdb->point_indexs[j];
|
||
std::vector<cv::Point2f> line;
|
||
for (unsigned int k = 0; k < point->points.size(); ++k) {
|
||
double real_x = point->points[k]->x;
|
||
double real_y = point->points[k]->y;
|
||
double mov_x1 = cpart->partPosBean.m_XOffset + point->points[k]->x;
|
||
double mov_y1 = cpart->partPosBean.m_YOffset + point->points[k]->y;
|
||
real_x = (mov_x1 - cpart->partPosBean.m_PartCenterX)*cos(cpart->partPosBean.m_Radians) - (mov_y1 - cpart->partPosBean.m_PartCenterY)*sin(cpart->partPosBean.m_Radians) + cpart->partPosBean.m_PartCenterX;
|
||
real_y = (mov_x1 - cpart->partPosBean.m_PartCenterX)*sin(cpart->partPosBean.m_Radians) + (mov_y1 - cpart->partPosBean.m_PartCenterY)*cos(cpart->partPosBean.m_Radians) + cpart->partPosBean.m_PartCenterY;
|
||
|
||
float tempx = real_x * m_CameraCalibrationCfg->m_MagnifyScale->GetValue() + centerx;
|
||
float tempy = centery - real_y * m_CameraCalibrationCfg->m_MagnifyScale->GetValue();
|
||
cv::Point2f p(tempx, tempy);
|
||
line.push_back(p);
|
||
}
|
||
int cc = j % 10;
|
||
if (!line.empty()) {
|
||
if (m_CameraCalibrationCfg->m_ShowAssist->GetValue()) {
|
||
cv::RotatedRect re = cv::minAreaRect(line);
|
||
cv::Point2f P[4];
|
||
re.points(P);
|
||
for (int j = 0; j <= 3; j++)
|
||
{
|
||
cv::line(m_GraftShowImage->m_ImageMat, P[j], P[(j + 1) % 4], m_ColorMap[cc], 1);
|
||
cv::circle(m_GraftShowImage->m_ImageMat, re.center, 1, m_ColorMap[cc]);
|
||
}
|
||
}
|
||
int pcx = cpart->partPosBean.m_PartCenterX*m_CameraCalibrationCfg->m_MagnifyScale->GetValue() + centerx + 0.5;
|
||
int pcy = centery - cpart->partPosBean.m_PartCenterY * m_CameraCalibrationCfg->m_MagnifyScale->GetValue() + 0.5;
|
||
for (size_t lindex = 0; lindex < line.size() - 1; lindex++) {
|
||
if (m_ManualGraftSelectPart == cpart->id) {
|
||
cv::line(m_GraftShowImage->m_ImageMat, line[lindex], line[lindex + 1], cv::Scalar(0, 255, 0), 1);
|
||
}
|
||
else {
|
||
cv::line(m_GraftShowImage->m_ImageMat, line[lindex], line[lindex + 1], cv::Scalar(255, 0, 0), 1);
|
||
}
|
||
|
||
}
|
||
//cv::circle(m_GraftShowImage->m_ImageMat, cv::Point(pcx, pcy), 1, cv::Scalar(255, 255, 255));
|
||
}
|
||
}
|
||
|
||
}
|
||
}
|
||
LeaveCriticalSection(&m_Jc->m_cs);
|
||
m_IsGraftUpdated = true;
|
||
}
|
||
}
|
||
|
||
void Calibration::StartGraft()
|
||
{
|
||
StopGraft();
|
||
m_GraftFlag = true;
|
||
m_GraftThread = AtlCreateThread(GraftProc, this);
|
||
}
|
||
|
||
void Calibration::StopGraft()
|
||
{
|
||
m_GraftFlag = false;
|
||
if (m_GraftThread != INVALID_HANDLE_VALUE) {
|
||
if (WaitForSingleObject(m_GraftThread, 1000) == WAIT_TIMEOUT) {
|
||
TerminateThread(m_GraftThread, 1);
|
||
}
|
||
CloseHandle(m_GraftThread);
|
||
m_GraftThread = INVALID_HANDLE_VALUE;
|
||
}
|
||
}
|
||
|
||
DWORD WINAPI Calibration::GraftProc(Calibration* _this)
|
||
{
|
||
if (_this) {
|
||
_this->GraftRunV2();
|
||
}
|
||
return 0;
|
||
}
|
||
|
||
void Calibration::GraftRunV2()
|
||
{
|
||
SetGraftInfo("");
|
||
SetGraftProgress(0.0f);
|
||
if (!m_CoverImage)return;
|
||
if (!m_BasePartImage)return;
|
||
|
||
stringstream ss;
|
||
char buffer[256];
|
||
|
||
SetGraftProgress(0.05f);
|
||
|
||
if (!m_GraftFlag)return;
|
||
|
||
cv::Mat divImage;
|
||
cv::Scalar partMean = cv::mean(m_BasePartImage->m_ImageMat);
|
||
cv::divide(m_BasePartImage->m_ImageMat, m_CoverImage->m_ImageMat, divImage, partMean.val[0]);
|
||
cv::imwrite("g:/TestGraf/div1.bmp", divImage);
|
||
|
||
cv::Mat divImage2;
|
||
cv::Scalar partMean2 = cv::mean(Calibration::m_CoverImage->m_ImageMat);
|
||
cv::divide(Calibration::m_CoverImage->m_ImageMat, Calibration::m_CoverImage->m_ImageMat, divImage2, partMean2.val[0]);
|
||
cv::imwrite("g:/TestGraf/div2.bmp", divImage2);
|
||
|
||
cv::Mat vertView=CalibrationWithCfg(divImage);
|
||
cv::imwrite("g:/TestGraf/view1.bmp", vertView);
|
||
cv::Mat vertView2 = CalibrationWithCfg(divImage2);
|
||
cv::imwrite("g:/TestGraf/view2.bmp", vertView2);
|
||
cv::Mat bg_mean, bg_stddev;
|
||
cv::meanStdDev(vertView2, bg_mean, bg_stddev);
|
||
double bgGrayAvg = 0.0;
|
||
bgGrayAvg = bg_mean.at<double>(0, 0);
|
||
vertView2.release();
|
||
divImage2.release();
|
||
|
||
cv::Mat dst;
|
||
cv::boxFilter(vertView, dst, -1, cv::Size(3, 3));
|
||
cv::imwrite("g:/TestGraf/box.bmp", dst);
|
||
cv::MatIterator_<uchar> it = dst.begin<uchar>();
|
||
cv::MatIterator_<uchar> end = dst.end<uchar>();
|
||
while (it != end)
|
||
{
|
||
double gray = *it;
|
||
if (gray > bgGrayAvg + m_CameraCalibrationCfg->m_BinaryThresholdOffset->GetValue() || gray < bgGrayAvg - m_CameraCalibrationCfg->m_BinaryThresholdOffset->GetValue()) {
|
||
*it = 255;
|
||
}
|
||
else {
|
||
*it = 0;
|
||
}
|
||
it++;
|
||
}
|
||
cv::imwrite("g:/TestGraf/bin.bmp", dst);
|
||
|
||
|
||
//cv::Mat element = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(4, 4));
|
||
//morphologyEx(dst, dst, cv::MORPH_CLOSE, element);
|
||
|
||
//cv::imwrite("g:/TestGraf/close.bmp", dst);
|
||
|
||
SetGraftProgress(0.1f);
|
||
if (!m_GraftFlag)return;
|
||
/*对寻找基座图轮廓*/
|
||
vector<vector<cv::Point>> contours;
|
||
vector<cv::Vec4i> hireachy;
|
||
|
||
cv::findContours(dst, contours, hireachy, cv::RETR_TREE, cv::CHAIN_APPROX_SIMPLE, cv::Point());
|
||
//cv::drawContours(vertView, contours, -1, cv::Scalar(255));
|
||
|
||
for (int i = 0; i < hireachy.size();i++) {
|
||
sprintf_s(buffer, sizeof(buffer), "%d %d %d %d \n", hireachy[i][0],hireachy[i][1],hireachy[i][2],hireachy[i][3]);
|
||
OutputDebugString(buffer);
|
||
vector<vector<cv::Point>> tempcons;
|
||
cv::drawContours(vertView, contours, -1, cv::Scalar(255));
|
||
}
|
||
|
||
|
||
//cv::imwrite("g:/TestGraf/ct.bmp", vertView);
|
||
if (!m_GraftFlag)return;
|
||
|
||
/* MetaData::Layer* layer = m_MetaData->GetLayer(0);
|
||
vector<MetaData::Part*> &partVec = m_MetaData->GetPartVec();
|
||
map<int, MatchInfo*> matchInfos;
|
||
int xLength = (m_CameraCalibrationCfg->m_PlatformTopRightX - m_CameraCalibrationCfg->m_PlatformTopLeftX)*m_CameraCalibrationCfg->m_MagnifyScale;
|
||
int yLength = (m_CameraCalibrationCfg->m_PlatformTopRightY - m_CameraCalibrationCfg->m_PlatformBottomRightY)*m_CameraCalibrationCfg->m_MagnifyScale;
|
||
int centerx = xLength / 2;
|
||
int centery = yLength / 2;
|
||
|
||
|
||
for (auto dbm : layer->data_blocks_map) {
|
||
if (!m_GraftFlag)return;
|
||
vector<MetaData::DataBlock*>* dbs = dbm.second;
|
||
MatchInfo* mi = new MatchInfo();
|
||
mi->m_PartId = dbm.first;
|
||
MetaData::Part* part = m_MetaData->GetPart(dbm.first);
|
||
cv::Mat imageTemp3(cv::Size(yLength, xLength), CV_8U, cv::Scalar::all(0));
|
||
//cv::Mat imageTempMat(cv::Size(xLength, yLength), CV_8U, cv::Scalar::all(0));
|
||
//vector<vector<cv::Point>> contoursTemp;
|
||
for (unsigned int dbIndex = 0; dbIndex < dbs->size(); ++dbIndex) {
|
||
MetaData::DataBlock* db = (*dbs)[dbIndex];
|
||
if (m_MetaData->m_FirstLayerBlockMap.find(db) == m_MetaData->m_FirstLayerBlockMap.end())continue;
|
||
BPBinary::BinDataBlock* pdb = m_MetaData->m_FirstLayerBlockMap[db];
|
||
if (!pdb)continue;
|
||
MetaData::ParameterSet* paramSet = m_MetaData->GetParameterSet(db->references->process);
|
||
if (!paramSet)continue;
|
||
if (pdb->type == BIN_VECTOR) {
|
||
for (unsigned int j = 0; j < pdb->point_indexs.size(); ++j) {
|
||
BPBinary::VectorPoint* pvp = (BPBinary::VectorPoint*)pdb->point_indexs[j];
|
||
float tempx1 = pvp->x1 * m_CameraCalibrationCfg->m_MagnifyScale + centerx;
|
||
float tempy1 = centery - pvp->y1 * m_CameraCalibrationCfg->m_MagnifyScale;
|
||
float tempx2 = pvp->x2 * m_CameraCalibrationCfg->m_MagnifyScale + centerx;
|
||
float tempy2 = centery - pvp->y2 * m_CameraCalibrationCfg->m_MagnifyScale;
|
||
cv::Point2f p1(tempx1, tempy1);
|
||
cv::Point2f p2(tempx2, tempy2);
|
||
cv::line(imageTemp3, p1, p2, cv::Scalar(255), 1);
|
||
}
|
||
}
|
||
else if (pdb->type == BIN_CHAIN) {
|
||
|
||
for (unsigned int j = 0; j < pdb->point_indexs.size(); ++j) {
|
||
std::vector<cv::Point> line;
|
||
BPBinary::ChainPoint* point = (BPBinary::ChainPoint*)pdb->point_indexs[j];
|
||
for (unsigned int k = 0; k < point->points.size(); ++k) {
|
||
float tempx = point->points[k]->x * m_CameraCalibrationCfg->m_MagnifyScale + centerx;
|
||
float tempy = centery - point->points[k]->y * m_CameraCalibrationCfg->m_MagnifyScale;
|
||
cv::Point2f p(tempx, tempy);
|
||
line.push_back(p);
|
||
}
|
||
if (!line.empty()) {
|
||
for (size_t lindex = 0; lindex < line.size() - 1; lindex++) {
|
||
cv::line(imageTemp3, line[lindex], line[lindex + 1], cv::Scalar(255), 1);
|
||
}
|
||
}
|
||
}
|
||
|
||
}
|
||
}
|
||
cv::Mat element2 = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(4, 4));
|
||
morphologyEx(imageTemp3, imageTemp3, cv::MORPH_CLOSE, element2);
|
||
imageTemp3.copyTo(mi->m_TplImage);
|
||
matchInfos[mi->m_PartId] = mi;
|
||
|
||
vector<vector<cv::Point>> contoursTemp;
|
||
vector<cv::Vec4i> hireachyTemp;
|
||
sprintf_s(buffer, sizeof(buffer), "%spart%d_hact.png", m_ImgPath.c_str(), mi->m_PartId);
|
||
cv::imwrite(buffer, imageTemp3);
|
||
|
||
cv::findContours(imageTemp3, contoursTemp, hireachyTemp, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_NONE, cv::Point());
|
||
imageTemp3.release();
|
||
//contoursTemp.push_back(line);
|
||
//mi->m_PartArea = abs(xMax - xMin) * abs(yMax - yMin);
|
||
mi->m_TplContours = contoursTemp[0];
|
||
//cv::Rect rt = cv::boundingRect(mi->m_TplContours);
|
||
cv::RotatedRect rt = cv::minAreaRect(contoursTemp[0]);
|
||
mi->m_TplCenter = rt.center;
|
||
mi->m_PartArea = cv::countNonZero(mi->m_TplImage);
|
||
//cv::Mat imageTemp4(cv::Size(yLength, xLength), CV_8U, cv::Scalar::all(0));
|
||
//cv::drawContours(imageTemp4, contoursTemp, -1, cv::Scalar(255), cv::FILLED);
|
||
//sprintf_s(buffer, sizeof(buffer), "%spart%d_bor.png", m_ImgPath.c_str(), mi->m_PartId);
|
||
//cv::imwrite(buffer, imageTemp4);
|
||
}
|
||
|
||
SetGraftProgress(0.15f);
|
||
|
||
|
||
map<int, bool> hasMatch;
|
||
for (auto mat : matchInfos) {
|
||
if (!m_GraftFlag)return;
|
||
double minRel = FLT_MAX;
|
||
int minIndex = -1;
|
||
|
||
for (size_t i = 0; i < contours.size(); i++) {
|
||
if (hasMatch.find(i) != hasMatch.end()) {
|
||
continue;
|
||
}
|
||
vector<LaserCfg*>* cfgs = ConfigManager::GetInstance()->GetMatchLaser();
|
||
cv::Rect rt = cv::boundingRect(contours[i]);
|
||
cv::Rect rt2 = cv::boundingRect(mat.second->m_TplContours);
|
||
int matchLaser = -1;
|
||
for (size_t lindex = 0; lindex < cfgs->size(); lindex++) {
|
||
float lminx = (*cfgs)[lindex]->m_ScanCfgWrapper.m_CorrectParamCfg.m_xmeasure_min*m_CameraCalibrationCfg->m_MagnifyScale + centerx;
|
||
float lmaxx = (*cfgs)[lindex]->m_ScanCfgWrapper.m_CorrectParamCfg.m_xmeasure_max*m_CameraCalibrationCfg->m_MagnifyScale + centerx;
|
||
float lmaxy = centery - (*cfgs)[lindex]->m_ScanCfgWrapper.m_CorrectParamCfg.m_ymeasure_min*m_CameraCalibrationCfg->m_MagnifyScale;
|
||
float lminy = centery - (*cfgs)[lindex]->m_ScanCfgWrapper.m_CorrectParamCfg.m_ymeasure_max*m_CameraCalibrationCfg->m_MagnifyScale;
|
||
|
||
float mminx = rt.x;
|
||
float mmaxx = rt.x + rt.width;
|
||
float mminy = rt.y;
|
||
float mmaxy = rt.y + rt.height;
|
||
if (mminx >lminx && lmaxx>mmaxx && mminy >lminy && lmaxy > mmaxy) {
|
||
matchLaser = (*cfgs)[lindex]->m_Cno;
|
||
break;
|
||
}
|
||
}
|
||
|
||
int dataLaser = -1;
|
||
for (size_t lindex = 0; lindex < cfgs->size(); lindex++) {
|
||
float lminx = (*cfgs)[lindex]->m_ScanCfgWrapper.m_CorrectParamCfg.m_xmeasure_min*m_CameraCalibrationCfg->m_MagnifyScale + centerx;
|
||
float lmaxx = (*cfgs)[lindex]->m_ScanCfgWrapper.m_CorrectParamCfg.m_xmeasure_max*m_CameraCalibrationCfg->m_MagnifyScale + centerx;
|
||
float lmaxy = centery - (*cfgs)[lindex]->m_ScanCfgWrapper.m_CorrectParamCfg.m_ymeasure_min*m_CameraCalibrationCfg->m_MagnifyScale;
|
||
float lminy = centery - (*cfgs)[lindex]->m_ScanCfgWrapper.m_CorrectParamCfg.m_ymeasure_max*m_CameraCalibrationCfg->m_MagnifyScale;
|
||
|
||
float mminx = rt2.x;
|
||
float mmaxx = rt2.x + rt2.width;
|
||
float mminy = rt2.y;
|
||
float mmaxy = rt2.y + rt2.height;
|
||
if (mminx > lminx && lmaxx > mmaxx && mminy > lminy && lmaxy > mmaxy) {
|
||
dataLaser = (*cfgs)[lindex]->m_Cno;
|
||
break;
|
||
}
|
||
}
|
||
if (matchLaser != dataLaser)continue;
|
||
vector<vector<cv::Point>> mct;
|
||
mct.push_back(contours[i]);
|
||
cv::Mat bortemp(cv::Size(yLength, xLength), CV_8U, cv::Scalar::all(0));
|
||
cv::drawContours(bortemp, mct, 0, cv::Scalar(255), -1);
|
||
cv::bitwise_and(dst, bortemp, bortemp);
|
||
float area = cv::countNonZero(bortemp);
|
||
if (abs(mat.second->m_PartArea - area) > (mat.second->m_PartArea*0.1f))
|
||
{
|
||
continue;
|
||
}
|
||
|
||
double v = cv::matchShapes(mat.second->m_TplContours, contours[i], cv::CONTOURS_MATCH_I2, 0);
|
||
if (v < minRel)
|
||
{
|
||
minRel = v;
|
||
minIndex = i;
|
||
}
|
||
}
|
||
if (minIndex != -1)
|
||
{
|
||
|
||
vector<vector<cv::Point>> contoursTemp;
|
||
contoursTemp.push_back(contours[minIndex]);
|
||
cv::Mat imageTemp22(cv::Size(yLength, xLength), CV_8U, cv::Scalar::all(0));
|
||
//cv::RotatedRect ctrr=cv::minAreaRect(contours[minIndex]);
|
||
//cv::Rect rt = cv::boundingRect(mi->m_TplContours);
|
||
//ct.push_back(ctrr.)
|
||
cv::drawContours(imageTemp22, contoursTemp, 0, cv::Scalar(255), -1);
|
||
cv::bitwise_and(dst, imageTemp22, imageTemp22);
|
||
|
||
// cv::drawContours(imageTemp22, contoursTemp, -1, cv::Scalar(255), cv::FILLED);
|
||
imageTemp22.copyTo(mat.second->m_MatchImage);
|
||
sprintf_s(buffer, sizeof(buffer), "%spart%d_match.png", m_ImgPath.c_str(), mat.first);
|
||
cv::imwrite(buffer, imageTemp22);
|
||
imageTemp22.release();
|
||
|
||
hasMatch[minIndex] = true;
|
||
mat.second->m_MatchContours = contours[minIndex];
|
||
cv::RotatedRect rt = cv::minAreaRect(contours[minIndex]);
|
||
mat.second->m_MatchCenter = rt.center;
|
||
}
|
||
else {
|
||
ss << u8"零件" << mat.first << u8"轮廓匹配失败\n";
|
||
}
|
||
}
|
||
|
||
dst.release();
|
||
SetGraftProgress(0.2f);
|
||
float step = 0.0f;
|
||
float prog = 0.2f;
|
||
if (matchInfos.empty()) {
|
||
SetGraftProgress(1.0f);
|
||
ss << u8"没有搜索到匹配信息\n";
|
||
return;
|
||
}
|
||
else {
|
||
step = 0.8f / matchInfos.size();
|
||
}
|
||
if (!m_GraftFlag)return;
|
||
cv::Ptr<cv::FeatureDetector> detector = cv::SIFT::create(500);
|
||
|
||
for (auto mat : matchInfos) {
|
||
if (!m_GraftFlag)break;
|
||
|
||
}
|
||
|
||
for (auto mat : matchInfos) {
|
||
if (!m_GraftFlag)break;
|
||
vector<cv::KeyPoint> tplKeyPoint;
|
||
vector<cv::KeyPoint> matchKeyPoint;
|
||
cv::Mat tplDesc;
|
||
cv::Mat matchDesc;
|
||
try
|
||
{
|
||
detector->detectAndCompute(mat.second->m_TplImage, cv::Mat(), tplKeyPoint, tplDesc);
|
||
detector->detectAndCompute(mat.second->m_MatchImage, cv::Mat(), matchKeyPoint, matchDesc);
|
||
cv::Ptr <cv::DescriptorMatcher > matcher = cv::BFMatcher::create();
|
||
|
||
vector<cv::DMatch> matchePoints;
|
||
vector<cv::DMatch> GoodMatchePoints;
|
||
matcher->match(matchDesc, tplDesc, matchePoints);
|
||
|
||
float distMax = -FLT_MAX;
|
||
for (size_t i = 0; i < matchePoints.size(); i++) {
|
||
if (matchePoints[i].distance > distMax) {
|
||
distMax = matchePoints[i].distance;
|
||
}
|
||
}
|
||
distMax = distMax*m_CameraCalibrationCfg->m_MatchRatio;
|
||
for (size_t i = 0; i < matchePoints.size(); i++) {
|
||
if (matchePoints[i].distance < distMax) {
|
||
GoodMatchePoints.push_back(matchePoints[i]);
|
||
}
|
||
}
|
||
|
||
if (!GoodMatchePoints.empty()) {
|
||
map<int, vector<float>> angleMap;
|
||
int count = 0;
|
||
for (int i = 0; i < GoodMatchePoints.size(); i++)
|
||
{
|
||
//int index = round(tplKeyPoint.at(GoodMatchePoints.at(i).trainIdx).angle - matchKeyPoint.at(GoodMatchePoints.at(i).queryIdx).angle);
|
||
float ang = tplKeyPoint.at(GoodMatchePoints.at(i).trainIdx).angle - matchKeyPoint.at(GoodMatchePoints.at(i).queryIdx).angle;
|
||
|
||
// index = index > 0 ? index : index + 360;
|
||
// if (index == 360) {
|
||
// OutputDebugString("123");
|
||
// }
|
||
ang = ang > 0.0f ? ang : ang + 360.0f;
|
||
int index = ceil(ang);
|
||
angleMap[index].push_back(ang);
|
||
}
|
||
int maxindex = -1;
|
||
int maxcount = 0;
|
||
for (auto ind : angleMap) {
|
||
if ((int)ind.second.size() >= maxcount)
|
||
{
|
||
maxindex = ind.first;
|
||
maxcount = ind.second.size();
|
||
}
|
||
}
|
||
float sum = 0.0f;
|
||
if (angleMap.find(maxindex) != angleMap.end()) {
|
||
for (auto andi : angleMap[maxindex]) {
|
||
sum += andi;
|
||
}
|
||
}
|
||
float avg = sum / angleMap[maxindex].size();
|
||
avg = (avg - 360.0f) < 0 ? avg : (avg - 360.0f);
|
||
mat.second->m_AimAngle = avg;
|
||
|
||
MetaData::Part* part = m_MetaData->GetPart(mat.first);
|
||
float partCenterX = part->partPosBean.m_SrcPartCenterX;
|
||
float partCenterY = part->partPosBean.m_SrcPartCenterY;
|
||
|
||
float tempx = partCenterX * m_CameraCalibrationCfg->m_MagnifyScale + centerx;
|
||
float tempy = (float)centery - partCenterY * m_CameraCalibrationCfg->m_MagnifyScale;
|
||
cv::Mat rot_mat = cv::getRotationMatrix2D(cv::Point2f(tempx, tempy), avg, 1.0);
|
||
cv::Mat tplr(mat.second->m_TplImage.size(), CV_8U);
|
||
cv::warpAffine(mat.second->m_TplImage, tplr, rot_mat, mat.second->m_TplImage.size(), cv::INTER_LINEAR, cv::BORDER_CONSTANT);
|
||
//sprintf_s(buffer, sizeof(buffer), "%spart%d_ttttt.png", m_ImgPath.c_str(), mat.first);
|
||
//cv::imwrite(buffer, tplr);
|
||
|
||
vector<vector<cv::Point>> contours0;
|
||
vector<cv::Vec4i> hireachy0;
|
||
cv::findContours(tplr, contours0, hireachy0, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_NONE, cv::Point());
|
||
|
||
cv::RotatedRect rotateRect0 = cv::minAreaRect(contours0[0]);
|
||
double cX = rotateRect0.center.x;
|
||
double cY = rotateRect0.center.y;
|
||
|
||
cv::RotatedRect rotateRect2 = cv::minAreaRect(mat.second->m_MatchContours);
|
||
double cX2 = rotateRect2.center.x;
|
||
double cY2 = rotateRect2.center.y;
|
||
|
||
mat.second->m_XOffset = cX2 - cX;
|
||
mat.second->m_YOffset = cY2 - cY;
|
||
|
||
//cv::Mat first_match;
|
||
//drawMatches(mat.second->m_MatchImage, matchKeyPoint, mat.second->m_TplImage, tplKeyPoint, GoodMatchePoints, first_match);
|
||
|
||
//sprintf_s(buffer, sizeof(buffer), "%spart%d_mat.png", m_ImgPath.c_str(), mat.first);
|
||
//cv::imwrite(buffer, first_match);
|
||
// cv::line(first_match, mat.second->m_TplCenter, mat.second->m_MatchCenter, cv::Scalar::all(255));
|
||
// cv::circle(first_match, mat.second->m_TplCenter, 2, cv::Scalar::all(255));
|
||
prog += step;
|
||
SetGraftProgress(prog);
|
||
}
|
||
matcher->clear();
|
||
}
|
||
catch (cv::Exception& e)
|
||
{
|
||
if (e.code == -4) {
|
||
ss << u8"零件" << mat.first << u8"自动嫁接失败:倍数过大\n";
|
||
}
|
||
else {
|
||
ss << u8"零件" << mat.first << u8"自动嫁接失败\n";
|
||
}
|
||
|
||
}
|
||
}
|
||
detector->clear();
|
||
if (!m_GraftFlag)return;
|
||
|
||
m_GraftShowImage->m_ImageMat.release();
|
||
m_GraftMat.copyTo(m_GraftShowImage->m_ImageMat);
|
||
cv::cvtColor(m_GraftShowImage->m_ImageMat, m_GraftShowImage->m_ImageMat, cv::COLOR_GRAY2RGB);
|
||
m_GraftShowImage->m_IsColor = true;
|
||
for (auto mat : matchInfos) {
|
||
MetaData::Part* part = m_MetaData->GetPart(mat.first);
|
||
if (part) {
|
||
part->partPosBean.m_XOffset = mat.second->m_XOffset / m_CameraCalibrationCfg->m_MagnifyScale;
|
||
part->partPosBean.m_YOffset = -mat.second->m_YOffset / m_CameraCalibrationCfg->m_MagnifyScale;
|
||
part->partPosBean.m_RotateAngle = mat.second->m_AimAngle;
|
||
part->partPosBean.m_Radians = (float)MathHelper::DegreesToRadians(part->partPosBean.m_RotateAngle);
|
||
part->partPosBean.m_PartCenterX = part->partPosBean.m_XOffset + part->partPosBean.m_SrcPartCenterX;
|
||
part->partPosBean.m_PartCenterY = part->partPosBean.m_YOffset + part->partPosBean.m_SrcPartCenterY;
|
||
|
||
Part* prev_part = m_PrevRenderer->GetPart(part->id);
|
||
if (prev_part)prev_part->UpdateOffset();
|
||
Part* print_part = m_Renderer->GetPart(part->id);
|
||
if (print_part)print_part->UpdateOffset();
|
||
}
|
||
|
||
for (auto dbm : layer->data_blocks_map) {
|
||
vector<MetaData::DataBlock*>* dbs = dbm.second;
|
||
MatchInfo* mi = matchInfos[dbm.first];
|
||
|
||
MetaData::Part* part = m_MetaData->GetPart(dbm.first);
|
||
//cv::Mat imageTemp3(cv::Size(yLength, xLength), CV_8U, cv::Scalar::all(0));
|
||
|
||
for (unsigned int dbIndex = 0; dbIndex < dbs->size(); ++dbIndex) {
|
||
MetaData::DataBlock* db = (*dbs)[dbIndex];
|
||
if (m_MetaData->m_FirstLayerBlockMap.find(db) == m_MetaData->m_FirstLayerBlockMap.end())continue;
|
||
if (db->references->part != mat.first)continue;
|
||
BPBinary::BinDataBlock* pdb = m_MetaData->m_FirstLayerBlockMap[db];
|
||
if (!pdb)continue;
|
||
if (pdb->type != BIN_CHAIN)continue;
|
||
|
||
MetaData::ParameterSet* paramSet = m_MetaData->GetParameterSet(db->references->process);
|
||
if (!paramSet)continue;
|
||
// if (paramSet->set_type == "InBorderAd")continue;
|
||
// if (paramSet->set_type.find("InBorder") == paramSet->set_type.npos)continue;
|
||
//MetaData::Part* part= m_MetaData->GetPart(db->references->part);
|
||
|
||
for (unsigned int j = 0; j < pdb->point_indexs.size(); ++j) {
|
||
BPBinary::ChainPoint* point = (BPBinary::ChainPoint*)pdb->point_indexs[j];
|
||
std::vector<cv::Point> line;
|
||
for (unsigned int k = 0; k < point->points.size(); ++k) {
|
||
double real_x = point->points[k]->x;
|
||
double real_y = point->points[k]->y;
|
||
|
||
double mov_x1 = part->partPosBean.m_XOffset + point->points[k]->x;
|
||
double mov_y1 = part->partPosBean.m_YOffset + point->points[k]->y;
|
||
real_x = (mov_x1 - part->partPosBean.m_PartCenterX)*cos(part->partPosBean.m_Radians) - (mov_y1 - part->partPosBean.m_PartCenterY)*sin(part->partPosBean.m_Radians) + part->partPosBean.m_PartCenterX;
|
||
real_y = (mov_x1 - part->partPosBean.m_PartCenterX)*sin(part->partPosBean.m_Radians) + (mov_y1 - part->partPosBean.m_PartCenterY)*cos(part->partPosBean.m_Radians) + part->partPosBean.m_PartCenterY;
|
||
|
||
float tempx = real_x * m_CameraCalibrationCfg->m_MagnifyScale + centerx;
|
||
float tempy = centery - real_y * m_CameraCalibrationCfg->m_MagnifyScale;
|
||
cv::Point2f p(tempx, tempy);
|
||
line.push_back(p);
|
||
|
||
}
|
||
for (size_t lindex = 0; lindex < line.size() - 1; lindex++) {
|
||
cv::line(m_GraftShowImage->m_ImageMat, line[lindex], line[lindex + 1], cv::Scalar(255));
|
||
}
|
||
}
|
||
|
||
sprintf_s(buffer, sizeof(buffer), "%sPart%d-bor.png", m_ImgPath.c_str(), dbm.first);
|
||
cv::imwrite(buffer, m_GraftShowImage->m_ImageMat);
|
||
}
|
||
}
|
||
}
|
||
|
||
//m_GraftShowImage->m_ImageMat.release();
|
||
// m_GraftMat.copyTo(m_GraftShowImage->m_ImageMat);
|
||
//sprintf_s(buffer, sizeof(buffer), "%sfin.png", m_ImgPath.c_str());
|
||
// imwrite(buffer, m_GraftShowImage->m_ImageMat);
|
||
|
||
*/
|
||
EnterCriticalSection(&m_GraftCS);
|
||
m_IsGraftUpdated = true;
|
||
LeaveCriticalSection(&m_GraftCS);
|
||
SetGraftProgress(1.0f);
|
||
ss << u8"自动嫁接完成\n";
|
||
SetGraftInfo(ss.str());
|
||
}
|
||
|
||
void Calibration::GraftRun()
|
||
{
|
||
SetGraftInfo("");
|
||
SetGraftProgress(0.0f);
|
||
if (!m_CoverImage)return;
|
||
if (!m_BasePartImage)return;
|
||
|
||
stringstream ss;
|
||
char buffer[256];
|
||
|
||
//SetGraftProgress(0.03f);
|
||
//cv::Mat bgFix = CalibrationWithCfg(m_CoverImage->m_ImageMat);
|
||
//sprintf_s(buffer, sizeof(buffer), "%sDivFix.bmp",m_ImgPath.c_str());
|
||
//cv::imwrite(buffer, bgFix);
|
||
SetGraftProgress(0.05f);
|
||
cv::Mat fix;
|
||
m_GraftMat.copyTo(fix);
|
||
|
||
if (!m_GraftFlag)return;
|
||
|
||
/*使用铺粉图亮度均值对基座图二值化*/
|
||
//cv::Mat bg_mean, bg_stddev;
|
||
//cv::meanStdDev(bgFix, bg_mean, bg_stddev);
|
||
//double bgGrayAvg = 0.0;
|
||
//bgGrayAvg = bg_mean.at<double>(0, 0);
|
||
cv::boxFilter(fix, fix, -1, cv::Size(3, 3));
|
||
SetGraftProgress(0.08f);
|
||
//cv::Mat element = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(4, 4));
|
||
//morphologyEx(fix, fix, cv::MORPH_CLOSE, element);
|
||
//sprintf_s(buffer, sizeof(buffer), "%sclose.bmp", m_ImgPath.c_str());
|
||
|
||
|
||
if (m_CameraCalibrationCfg->m_BlackFace->GetValue()) {
|
||
cv::threshold(fix, fix, m_CameraCalibrationCfg->m_GrayRef->GetValue() - m_CameraCalibrationCfg->m_BinaryThresholdOffset->GetValue(), 255, cv::ThresholdTypes::THRESH_BINARY_INV);
|
||
}
|
||
else {
|
||
cv::threshold(fix, fix, m_CameraCalibrationCfg->m_GrayRef->GetValue() + m_CameraCalibrationCfg->m_BinaryThresholdOffset->GetValue(), 255, cv::ThresholdTypes::THRESH_BINARY);
|
||
}
|
||
sprintf_s(buffer, sizeof(buffer), "%sClose.bmp", m_ImgPath.c_str());
|
||
cv::imwrite(buffer, fix);
|
||
cv::Mat element = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(4, 4));
|
||
morphologyEx(fix, fix, cv::MORPH_CLOSE, element);
|
||
sprintf_s(buffer, sizeof(buffer), "%sDivBin.bmp", m_ImgPath.c_str());
|
||
cv::imwrite(buffer, fix);
|
||
SetGraftProgress(0.1f);
|
||
if (!m_GraftFlag)return;
|
||
/*对寻找基座图轮廓*/
|
||
vector<vector<cv::Point>> contours;
|
||
vector<cv::Vec4i> hireachy;
|
||
cv::findContours(fix, contours, hireachy, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_NONE, cv::Point());
|
||
|
||
|
||
if (!m_GraftFlag)return;
|
||
|
||
MetaData::Layer* layer = m_MetaData->GetLayer(0);
|
||
vector<MetaData::Part*> &partVec = m_MetaData->GetPartVec();
|
||
map<int, MatchInfo*> matchInfos;
|
||
int xLength = (m_CameraCalibrationCfg->m_PlatformTopRightX->GetValue() - m_CameraCalibrationCfg->m_PlatformTopLeftX->GetValue())*m_CameraCalibrationCfg->m_MagnifyScale->GetValue();
|
||
int yLength = (m_CameraCalibrationCfg->m_PlatformTopRightY->GetValue() - m_CameraCalibrationCfg->m_PlatformBottomRightY->GetValue())*m_CameraCalibrationCfg->m_MagnifyScale->GetValue();
|
||
int centerx = xLength / 2;
|
||
int centery = yLength / 2;
|
||
|
||
/*数据零件实体图*/
|
||
for (auto dbm : layer->data_blocks_map) {
|
||
if (!m_GraftFlag)return;
|
||
vector<MetaData::DataBlock*>* dbs = dbm.second;
|
||
MatchInfo* mi = new MatchInfo();
|
||
mi->m_PartId = dbm.first;
|
||
MetaData::Part* part = m_MetaData->GetPart(dbm.first);
|
||
cv::Mat imageTemp3(cv::Size(yLength, xLength), CV_8U, cv::Scalar::all(0));
|
||
//cv::Mat imageTempMat(cv::Size(xLength, yLength), CV_8U, cv::Scalar::all(0));
|
||
//vector<vector<cv::Point>> contoursTemp;
|
||
for (unsigned int dbIndex = 0; dbIndex < dbs->size(); ++dbIndex) {
|
||
MetaData::DataBlock* db = (*dbs)[dbIndex];
|
||
if (m_MetaData->m_FirstLayerBlockMap.find(db) == m_MetaData->m_FirstLayerBlockMap.end())continue;
|
||
BPBinary::BinDataBlock* pdb = m_MetaData->m_FirstLayerBlockMap[db];
|
||
if (!pdb)continue;
|
||
MetaData::ParameterSet* paramSet = m_MetaData->GetParameterSet(db->references->process);
|
||
if (!paramSet)continue;
|
||
if (pdb->type == BIN_VECTOR) {
|
||
for (unsigned int j = 0; j < pdb->point_indexs.size(); ++j) {
|
||
BPBinary::VectorPoint* pvp = (BPBinary::VectorPoint*)pdb->point_indexs[j];
|
||
float tempx1 = pvp->x1 * m_CameraCalibrationCfg->m_MagnifyScale->GetValue() + centerx;
|
||
float tempy1 = centery - pvp->y1 * m_CameraCalibrationCfg->m_MagnifyScale->GetValue();
|
||
float tempx2 = pvp->x2 * m_CameraCalibrationCfg->m_MagnifyScale->GetValue() + centerx;
|
||
float tempy2 = centery - pvp->y2 * m_CameraCalibrationCfg->m_MagnifyScale->GetValue();
|
||
cv::Point2f p1(tempx1, tempy1);
|
||
cv::Point2f p2(tempx2, tempy2);
|
||
cv::line(imageTemp3, p1, p2, cv::Scalar(255), 1);
|
||
}
|
||
}
|
||
else if (pdb->type == BIN_CHAIN) {
|
||
|
||
for (unsigned int j = 0; j < pdb->point_indexs.size(); ++j) {
|
||
std::vector<cv::Point> line;
|
||
BPBinary::ChainPoint* point = (BPBinary::ChainPoint*)pdb->point_indexs[j];
|
||
for (unsigned int k = 0; k < point->points.size(); ++k) {
|
||
float tempx = point->points[k]->x * m_CameraCalibrationCfg->m_MagnifyScale->GetValue() + centerx;
|
||
float tempy = centery - point->points[k]->y * m_CameraCalibrationCfg->m_MagnifyScale->GetValue();
|
||
cv::Point2f p(tempx, tempy);
|
||
line.push_back(p);
|
||
}
|
||
if (!line.empty()) {
|
||
for (size_t lindex = 0; lindex < line.size() - 1; lindex++) {
|
||
cv::line(imageTemp3, line[lindex], line[lindex + 1], cv::Scalar(255), 1);
|
||
}
|
||
}
|
||
}
|
||
|
||
}
|
||
}
|
||
cv::Mat element2 = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(4, 4));
|
||
morphologyEx(imageTemp3, imageTemp3, cv::MORPH_CLOSE, element2);
|
||
imageTemp3.copyTo(mi->m_TplImage);
|
||
matchInfos[mi->m_PartId] = mi;
|
||
|
||
vector<vector<cv::Point>> contoursTemp;
|
||
vector<cv::Vec4i> hireachyTemp;
|
||
sprintf_s(buffer, sizeof(buffer), "%spart%d_hact.png", m_ImgPath.c_str(), mi->m_PartId);
|
||
cv::imwrite(buffer, imageTemp3);
|
||
|
||
cv::findContours(imageTemp3, contoursTemp, hireachyTemp, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_NONE, cv::Point());
|
||
imageTemp3.release();
|
||
//contoursTemp.push_back(line);
|
||
//mi->m_PartArea = abs(xMax - xMin) * abs(yMax - yMin);
|
||
mi->m_TplContours = contoursTemp[0];
|
||
//cv::Rect rt = cv::boundingRect(mi->m_TplContours);
|
||
|
||
mi->m_PartArea = cv::countNonZero(mi->m_TplImage);
|
||
//cv::Mat imageTemp4(cv::Size(yLength, xLength), CV_8U, cv::Scalar::all(0));
|
||
//cv::drawContours(imageTemp4, contoursTemp, -1, cv::Scalar(255), cv::FILLED);
|
||
//sprintf_s(buffer, sizeof(buffer), "%spart%d_bor.png", m_ImgPath.c_str(), mi->m_PartId);
|
||
//cv::imwrite(buffer, imageTemp4);
|
||
}
|
||
|
||
SetGraftProgress(0.12f);
|
||
/*数据零件轮廓图*/
|
||
/*for (auto dbm : layer->data_blocks_map) {
|
||
if (!m_GraftFlag)return;
|
||
vector<MetaData::DataBlock*>* dbs = dbm.second;
|
||
MatchInfo* mi = matchInfos[dbm.first];
|
||
MetaData::Part* part = m_MetaData->GetPart(dbm.first);
|
||
float xMin = FLT_MAX, xMax = -FLT_MAX, yMin = FLT_MAX, yMax = -FLT_MAX;
|
||
// cv::Mat imageTemp3(cv::Size(yLength, xLength), CV_8U, cv::Scalar::all(0));
|
||
vector<vector<cv::Point>> contoursTemp;
|
||
std::vector<cv::Point> line;
|
||
for (unsigned int dbIndex = 0; dbIndex < dbs->size(); ++dbIndex) {
|
||
MetaData::DataBlock* db = (*dbs)[dbIndex];
|
||
if (m_MetaData->m_FirstLayerBlockMap.find(db) == m_MetaData->m_FirstLayerBlockMap.end())continue;
|
||
BPBinary::BinDataBlock* pdb = m_MetaData->m_FirstLayerBlockMap[db];
|
||
if (!pdb)continue;
|
||
if (pdb->type != BIN_CHAIN)continue;
|
||
MetaData::ParameterSet* paramSet = m_MetaData->GetParameterSet(db->references->process);
|
||
if (!paramSet)continue;
|
||
if (paramSet->set_type == "InBorderAd")continue;
|
||
if (paramSet->set_type.find("InBorder") == paramSet->set_type.npos)continue;
|
||
//MetaData::Part* part= m_MetaData->GetPart(db->references->part);
|
||
|
||
for (unsigned int j = 0; j < pdb->point_indexs.size(); ++j) {
|
||
BPBinary::ChainPoint* point = (BPBinary::ChainPoint*)pdb->point_indexs[j];
|
||
for (unsigned int k = 0; k < point->points.size(); ++k) {
|
||
float tempx = point->points[k]->x * m_CameraCalibrationCfg->m_MagnifyScale + centerx;
|
||
float tempy = centery - point->points[k]->y * m_CameraCalibrationCfg->m_MagnifyScale;
|
||
cv::Point2f p(tempx, tempy);
|
||
line.push_back(p);
|
||
if (tempx < xMin)xMin = tempx;
|
||
if (tempx > xMax)xMax = tempx;
|
||
if (tempy < yMin)yMin = tempy;
|
||
if (tempy > yMax)yMax = tempy;
|
||
}
|
||
}
|
||
}
|
||
//m_GraftBorderSrcData[dbm.first] = line;
|
||
contoursTemp.push_back(line);
|
||
mi->m_PartArea = abs(xMax - xMin) * abs(yMax - yMin);
|
||
mi->m_TplContours = contoursTemp[0];
|
||
}*/
|
||
SetGraftProgress(0.15f);
|
||
/*基座轮廓与数据零件轮廓匹配*/
|
||
|
||
map<int, bool> hasMatch;
|
||
for (auto mat : matchInfos) {
|
||
if (!m_GraftFlag)return;
|
||
double minRel = FLT_MAX;
|
||
int minIndex = -1;
|
||
|
||
for (size_t i = 0; i < contours.size(); i++) {
|
||
if (hasMatch.find(i) != hasMatch.end()) {
|
||
continue;
|
||
}
|
||
vector<ScannerControlCfg*>* cfgs = ConfigManager::GetInstance()->GetMatchScannerControlCfg();
|
||
cv::Rect rt = cv::boundingRect(contours[i]);
|
||
cv::Rect rt2 = cv::boundingRect(mat.second->m_TplContours);
|
||
int matchLaser = -1;
|
||
for (size_t lindex = 0; lindex < cfgs->size(); lindex++) {
|
||
float lminx = (*cfgs)[lindex]->m_ScanCfgWrapper.m_CorrectParamCfg.m_xmeasure_min*m_CameraCalibrationCfg->m_MagnifyScale->GetValue() + centerx;
|
||
float lmaxx = (*cfgs)[lindex]->m_ScanCfgWrapper.m_CorrectParamCfg.m_xmeasure_max*m_CameraCalibrationCfg->m_MagnifyScale->GetValue() + centerx;
|
||
float lmaxy = centery - (*cfgs)[lindex]->m_ScanCfgWrapper.m_CorrectParamCfg.m_ymeasure_min*m_CameraCalibrationCfg->m_MagnifyScale->GetValue();
|
||
float lminy = centery - (*cfgs)[lindex]->m_ScanCfgWrapper.m_CorrectParamCfg.m_ymeasure_max*m_CameraCalibrationCfg->m_MagnifyScale->GetValue();
|
||
|
||
float mminx = rt.x;
|
||
float mmaxx = rt.x + rt.width;
|
||
float mminy = rt.y;
|
||
float mmaxy = rt.y + rt.height;
|
||
if (mminx >lminx && lmaxx>mmaxx && mminy >lminy && lmaxy > mmaxy) {
|
||
matchLaser = (*cfgs)[lindex]->m_SeqNo->GetValue();
|
||
break;
|
||
}
|
||
}
|
||
|
||
int dataLaser = -1;
|
||
for (size_t lindex = 0; lindex < cfgs->size(); lindex++) {
|
||
float lminx = (*cfgs)[lindex]->m_ScanCfgWrapper.m_CorrectParamCfg.m_xmeasure_min*m_CameraCalibrationCfg->m_MagnifyScale->GetValue() + centerx;
|
||
float lmaxx = (*cfgs)[lindex]->m_ScanCfgWrapper.m_CorrectParamCfg.m_xmeasure_max*m_CameraCalibrationCfg->m_MagnifyScale->GetValue() + centerx;
|
||
float lmaxy = centery - (*cfgs)[lindex]->m_ScanCfgWrapper.m_CorrectParamCfg.m_ymeasure_min*m_CameraCalibrationCfg->m_MagnifyScale->GetValue();
|
||
float lminy = centery - (*cfgs)[lindex]->m_ScanCfgWrapper.m_CorrectParamCfg.m_ymeasure_max*m_CameraCalibrationCfg->m_MagnifyScale->GetValue();
|
||
|
||
float mminx = rt2.x;
|
||
float mmaxx = rt2.x + rt2.width;
|
||
float mminy = rt2.y;
|
||
float mmaxy = rt2.y + rt2.height;
|
||
if (mminx > lminx && lmaxx > mmaxx && mminy > lminy && lmaxy > mmaxy) {
|
||
dataLaser = (*cfgs)[lindex]->m_SeqNo->GetValue();
|
||
break;
|
||
}
|
||
}
|
||
if (matchLaser != dataLaser)continue;
|
||
vector<vector<cv::Point>> mct;
|
||
mct.push_back(contours[i]);
|
||
cv::Mat bortemp(cv::Size(yLength, xLength), CV_8U, cv::Scalar::all(0));
|
||
cv::drawContours(bortemp, mct, 0, cv::Scalar(255), -1);
|
||
cv::bitwise_and(fix, bortemp, bortemp);
|
||
float area = cv::countNonZero(bortemp);
|
||
if (abs(mat.second->m_PartArea - area) > (mat.second->m_PartArea*0.1f))
|
||
{
|
||
continue;
|
||
}
|
||
|
||
double v = cv::matchShapes(mat.second->m_TplContours, contours[i], cv::CONTOURS_MATCH_I2, 0);
|
||
if (v < minRel)
|
||
{
|
||
minRel = v;
|
||
minIndex = i;
|
||
}
|
||
}
|
||
if (minIndex != -1)
|
||
{
|
||
|
||
vector<vector<cv::Point>> contoursTemp;
|
||
contoursTemp.push_back(contours[minIndex]);
|
||
cv::Mat imageTemp22(cv::Size(yLength, xLength), CV_8U, cv::Scalar::all(0));
|
||
//cv::RotatedRect ctrr=cv::minAreaRect(contours[minIndex]);
|
||
//cv::Rect rt = cv::boundingRect(mi->m_TplContours);
|
||
//ct.push_back(ctrr.)
|
||
cv::drawContours(imageTemp22, contoursTemp, 0, cv::Scalar(255), -1);
|
||
cv::bitwise_and(fix, imageTemp22, imageTemp22);
|
||
|
||
// cv::drawContours(imageTemp22, contoursTemp, -1, cv::Scalar(255), cv::FILLED);
|
||
imageTemp22.copyTo(mat.second->m_MatchImage);
|
||
sprintf_s(buffer, sizeof(buffer), "%spart%d_match.png", m_ImgPath.c_str(), mat.first);
|
||
cv::imwrite(buffer, imageTemp22);
|
||
imageTemp22.release();
|
||
|
||
hasMatch[minIndex] = true;
|
||
mat.second->m_MatchContours = contours[minIndex];
|
||
|
||
//cv::Moments mom = cv::moments(mat.second->m_TplContours, true);
|
||
//double cX = mom.m10 / mom.m00;
|
||
//double cY = mom.m01 / mom.m00;
|
||
|
||
|
||
//cv::Moments mom2 = cv::moments(mat.second->m_MatchContours, true);
|
||
//double cX2 = mom2.m10 / mom2.m00;
|
||
//double cY2 = mom2.m01 / mom2.m00;
|
||
|
||
/* cv::RotatedRect rotateRect = cv::minAreaRect(mat.second->m_TplContours);
|
||
double cX = rotateRect.center.x;
|
||
double cY = rotateRect.center.y;
|
||
mat.second->m_TplCenter = cv::Point2f(cX, cY);
|
||
|
||
cv::RotatedRect rotateRect2 = cv::minAreaRect(mat.second->m_MatchContours);
|
||
double cX2 = rotateRect2.center.x;
|
||
double cY2 = rotateRect2.center.y;
|
||
mat.second->m_MatchCenter = cv::Point2f(cX2, cY2);
|
||
|
||
cv::Mat temp;
|
||
mat.second->m_TplImage.copyTo(temp);
|
||
cv::cvtColor(temp, temp, CV_GRAY2RGB);
|
||
cv::circle(temp, mat.second->m_TplCenter, 1, cv::Scalar(0,255,0,0));
|
||
cv::drawContours(temp, contoursTemp, -1, cv::Scalar(255,0,0,0));
|
||
cv::circle(temp, mat.second->m_MatchCenter, 1, cv::Scalar(255,0,0,0));
|
||
|
||
MetaData::Part* part = m_MetaData->GetPart(mat.first);
|
||
float partCenterX = (part->dimensions->xmax + part->dimensions->xmin) / 2.0f;
|
||
float partCenterY = (part->dimensions->ymax + part->dimensions->ymin) / 2.0f;
|
||
|
||
float tempx = partCenterX * m_CameraCalibrationCfg->m_MagnifyScale + centerx;
|
||
float tempy = centery - partCenterY * m_CameraCalibrationCfg->m_MagnifyScale;
|
||
|
||
cv::circle(temp, cv::Point2f(tempx, tempy), 1, cv::Scalar(0, 0, 255, 0));
|
||
sprintf_s(buffer, sizeof(buffer), "%spart%d_join.png", m_ImgPath.c_str(), mat.first);
|
||
cv::imwrite(buffer, temp);*/
|
||
}
|
||
else {
|
||
ss << u8"零件" << mat.first << u8"轮廓匹配失败\n";
|
||
}
|
||
}
|
||
|
||
fix.release();
|
||
SetGraftProgress(0.2f);
|
||
float step = 0.0f;
|
||
float prog = 0.2f;
|
||
if (matchInfos.empty()) {
|
||
SetGraftProgress(1.0f);
|
||
ss << u8"没有搜索到匹配信息\n";
|
||
return;
|
||
}
|
||
else {
|
||
step = 0.8f / matchInfos.size();
|
||
}
|
||
if (!m_GraftFlag)return;
|
||
cv::Ptr<cv::FeatureDetector> detector = cv::SIFT::create(500);
|
||
|
||
for (auto mat : matchInfos) {
|
||
if (!m_GraftFlag)break;
|
||
vector<cv::KeyPoint> tplKeyPoint;
|
||
vector<cv::KeyPoint> matchKeyPoint;
|
||
cv::Mat tplDesc;
|
||
cv::Mat matchDesc;
|
||
try
|
||
{
|
||
detector->detectAndCompute(mat.second->m_TplImage, cv::Mat(), tplKeyPoint, tplDesc);
|
||
detector->detectAndCompute(mat.second->m_MatchImage, cv::Mat(), matchKeyPoint, matchDesc);
|
||
cv::Ptr <cv::DescriptorMatcher > matcher = cv::BFMatcher::create();
|
||
|
||
vector<cv::DMatch> matchePoints;
|
||
vector<cv::DMatch> GoodMatchePoints;
|
||
matcher->match(matchDesc, tplDesc, matchePoints);
|
||
|
||
float distMax = -FLT_MAX;
|
||
for (size_t i = 0; i < matchePoints.size(); i++) {
|
||
if (matchePoints[i].distance > distMax) {
|
||
distMax = matchePoints[i].distance;
|
||
}
|
||
}
|
||
distMax = distMax*m_CameraCalibrationCfg->m_MatchRatio->GetValue();
|
||
for (size_t i = 0; i < matchePoints.size(); i++) {
|
||
if (matchePoints[i].distance < distMax) {
|
||
GoodMatchePoints.push_back(matchePoints[i]);
|
||
}
|
||
}
|
||
|
||
if (!GoodMatchePoints.empty()) {
|
||
map<int, vector<float>> angleMap;
|
||
int count = 0;
|
||
for (int i = 0; i < GoodMatchePoints.size(); i++)
|
||
{
|
||
//int index = round(tplKeyPoint.at(GoodMatchePoints.at(i).trainIdx).angle - matchKeyPoint.at(GoodMatchePoints.at(i).queryIdx).angle);
|
||
float ang = tplKeyPoint.at(GoodMatchePoints.at(i).trainIdx).angle - matchKeyPoint.at(GoodMatchePoints.at(i).queryIdx).angle;
|
||
|
||
// index = index > 0 ? index : index + 360;
|
||
// if (index == 360) {
|
||
// OutputDebugString("123");
|
||
// }
|
||
ang = ang > 0.0f ? ang : ang + 360.0f;
|
||
int index = ceil(ang);
|
||
angleMap[index].push_back(ang);
|
||
}
|
||
int maxindex = -1;
|
||
int maxcount = 0;
|
||
for (auto ind : angleMap) {
|
||
if ((int)ind.second.size() >= maxcount)
|
||
{
|
||
maxindex = ind.first;
|
||
maxcount = ind.second.size();
|
||
}
|
||
}
|
||
float sum = 0.0f;
|
||
if (angleMap.find(maxindex) != angleMap.end()) {
|
||
for (auto andi : angleMap[maxindex]) {
|
||
sum += andi;
|
||
}
|
||
}
|
||
float avg = sum / angleMap[maxindex].size();
|
||
avg = (avg - 360.0f) < 0 ? avg : (avg - 360.0f);
|
||
mat.second->m_AimAngle = avg;
|
||
|
||
MetaData::Part* part = m_MetaData->GetPart(mat.first);
|
||
float partCenterX = part->partPosBean.m_SrcPartCenterX;
|
||
float partCenterY = part->partPosBean.m_SrcPartCenterY;
|
||
|
||
float tempx = partCenterX * m_CameraCalibrationCfg->m_MagnifyScale->GetValue() + centerx;
|
||
float tempy = (float)centery - partCenterY * m_CameraCalibrationCfg->m_MagnifyScale->GetValue();
|
||
cv::Mat rot_mat = cv::getRotationMatrix2D(cv::Point2f(tempx, tempy), avg, 1.0);
|
||
cv::Mat tplr(mat.second->m_TplImage.size(), CV_8U);
|
||
cv::warpAffine(mat.second->m_TplImage, tplr, rot_mat, mat.second->m_TplImage.size(), cv::INTER_LINEAR, cv::BORDER_CONSTANT);
|
||
//sprintf_s(buffer, sizeof(buffer), "%spart%d_ttttt.png", m_ImgPath.c_str(), mat.first);
|
||
//cv::imwrite(buffer, tplr);
|
||
|
||
vector<vector<cv::Point>> contours0;
|
||
vector<cv::Vec4i> hireachy0;
|
||
cv::findContours(tplr, contours0, hireachy0, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_NONE, cv::Point());
|
||
|
||
cv::RotatedRect rotateRect0 = cv::minAreaRect(contours0[0]);
|
||
double cX = rotateRect0.center.x;
|
||
double cY = rotateRect0.center.y;
|
||
|
||
cv::RotatedRect rotateRect2 = cv::minAreaRect(mat.second->m_MatchContours);
|
||
double cX2 = rotateRect2.center.x;
|
||
double cY2 = rotateRect2.center.y;
|
||
|
||
mat.second->m_XOffset = cX2 - cX;
|
||
mat.second->m_YOffset = cY2 - cY;
|
||
|
||
//cv::Mat first_match;
|
||
//drawMatches(mat.second->m_MatchImage, matchKeyPoint, mat.second->m_TplImage, tplKeyPoint, GoodMatchePoints, first_match);
|
||
|
||
//sprintf_s(buffer, sizeof(buffer), "%spart%d_mat.png", m_ImgPath.c_str(), mat.first);
|
||
//cv::imwrite(buffer, first_match);
|
||
// cv::line(first_match, mat.second->m_TplCenter, mat.second->m_MatchCenter, cv::Scalar::all(255));
|
||
// cv::circle(first_match, mat.second->m_TplCenter, 2, cv::Scalar::all(255));
|
||
prog += step;
|
||
SetGraftProgress(prog);
|
||
}
|
||
matcher->clear();
|
||
}
|
||
catch (cv::Exception& e)
|
||
{
|
||
if (e.code == -4) {
|
||
ss << u8"零件" << mat.first << u8"自动嫁接失败:倍数过大\n";
|
||
}
|
||
else {
|
||
ss << u8"零件" << mat.first << u8"自动嫁接失败\n";
|
||
}
|
||
|
||
}
|
||
}
|
||
detector->clear();
|
||
if (!m_GraftFlag)return;
|
||
|
||
m_GraftShowImage->m_ImageMat.release();
|
||
m_GraftMat.copyTo(m_GraftShowImage->m_ImageMat);
|
||
cv::cvtColor(m_GraftShowImage->m_ImageMat, m_GraftShowImage->m_ImageMat, cv::COLOR_GRAY2RGB);
|
||
m_GraftShowImage->m_IsColor = true;
|
||
for (auto mat : matchInfos) {
|
||
MetaData::Part* part = m_MetaData->GetPart(mat.first);
|
||
if (part) {
|
||
part->partPosBean.m_XOffset = mat.second->m_XOffset / m_CameraCalibrationCfg->m_MagnifyScale->GetValue();
|
||
part->partPosBean.m_YOffset = -mat.second->m_YOffset / m_CameraCalibrationCfg->m_MagnifyScale->GetValue();
|
||
part->partPosBean.m_RotateAngle = mat.second->m_AimAngle;
|
||
part->partPosBean.m_Radians = (float)MathHelper::DegreesToRadians(part->partPosBean.m_RotateAngle);
|
||
part->partPosBean.m_PartCenterX = part->partPosBean.m_XOffset + part->partPosBean.m_SrcPartCenterX;
|
||
part->partPosBean.m_PartCenterY = part->partPosBean.m_YOffset + part->partPosBean.m_SrcPartCenterY;
|
||
|
||
//Part* prev_part = m_PrevRenderer->GetPart(part->id); //wxxtest
|
||
//if (prev_part)prev_part->UpdateOffset();
|
||
//Part* print_part = m_Renderer->GetPart(part->id);
|
||
//if (print_part)print_part->UpdateOffset();
|
||
}
|
||
|
||
for (auto dbm : layer->data_blocks_map) {
|
||
vector<MetaData::DataBlock*>* dbs = dbm.second;
|
||
MatchInfo* mi = matchInfos[dbm.first];
|
||
|
||
MetaData::Part* part = m_MetaData->GetPart(dbm.first);
|
||
//cv::Mat imageTemp3(cv::Size(yLength, xLength), CV_8U, cv::Scalar::all(0));
|
||
|
||
for (unsigned int dbIndex = 0; dbIndex < dbs->size(); ++dbIndex) {
|
||
MetaData::DataBlock* db = (*dbs)[dbIndex];
|
||
if (m_MetaData->m_FirstLayerBlockMap.find(db) == m_MetaData->m_FirstLayerBlockMap.end())continue;
|
||
if (db->references->part != mat.first)continue;
|
||
BPBinary::BinDataBlock* pdb = m_MetaData->m_FirstLayerBlockMap[db];
|
||
if (!pdb)continue;
|
||
if (pdb->type != BIN_CHAIN)continue;
|
||
|
||
MetaData::ParameterSet* paramSet = m_MetaData->GetParameterSet(db->references->process);
|
||
if (!paramSet)continue;
|
||
// if (paramSet->set_type == "InBorderAd")continue;
|
||
// if (paramSet->set_type.find("InBorder") == paramSet->set_type.npos)continue;
|
||
//MetaData::Part* part= m_MetaData->GetPart(db->references->part);
|
||
|
||
for (unsigned int j = 0; j < pdb->point_indexs.size(); ++j) {
|
||
BPBinary::ChainPoint* point = (BPBinary::ChainPoint*)pdb->point_indexs[j];
|
||
std::vector<cv::Point> line;
|
||
for (unsigned int k = 0; k < point->points.size(); ++k) {
|
||
double real_x = point->points[k]->x;
|
||
double real_y = point->points[k]->y;
|
||
|
||
double mov_x1 = part->partPosBean.m_XOffset + point->points[k]->x;
|
||
double mov_y1 = part->partPosBean.m_YOffset + point->points[k]->y;
|
||
real_x = (mov_x1 - part->partPosBean.m_PartCenterX)*cos(part->partPosBean.m_Radians) - (mov_y1 - part->partPosBean.m_PartCenterY)*sin(part->partPosBean.m_Radians) + part->partPosBean.m_PartCenterX;
|
||
real_y = (mov_x1 - part->partPosBean.m_PartCenterX)*sin(part->partPosBean.m_Radians) + (mov_y1 - part->partPosBean.m_PartCenterY)*cos(part->partPosBean.m_Radians) + part->partPosBean.m_PartCenterY;
|
||
|
||
float tempx = real_x * m_CameraCalibrationCfg->m_MagnifyScale->GetValue() + centerx;
|
||
float tempy = centery - real_y * m_CameraCalibrationCfg->m_MagnifyScale->GetValue();
|
||
cv::Point2f p(tempx, tempy);
|
||
line.push_back(p);
|
||
|
||
}
|
||
for (size_t lindex = 0; lindex < line.size() - 1; lindex++) {
|
||
cv::line(m_GraftShowImage->m_ImageMat, line[lindex], line[lindex + 1], cv::Scalar(255));
|
||
}
|
||
}
|
||
|
||
sprintf_s(buffer, sizeof(buffer), "%sPart%d-bor.png", m_ImgPath.c_str(), dbm.first);
|
||
cv::imwrite(buffer, m_GraftShowImage->m_ImageMat);
|
||
}
|
||
}
|
||
}
|
||
|
||
//m_GraftShowImage->m_ImageMat.release();
|
||
// m_GraftMat.copyTo(m_GraftShowImage->m_ImageMat);
|
||
//sprintf_s(buffer, sizeof(buffer), "%sfin.png", m_ImgPath.c_str());
|
||
// imwrite(buffer, m_GraftShowImage->m_ImageMat);
|
||
EnterCriticalSection(&m_GraftCS);
|
||
m_IsGraftUpdated = true;
|
||
LeaveCriticalSection(&m_GraftCS);
|
||
SetGraftProgress(1.0f);
|
||
ss << u8"自动嫁接完成\n";
|
||
SetGraftInfo(ss.str());
|
||
}
|
||
|
||
cv::Mat Calibration::CalibrationImage(cv::Mat image)
|
||
{
|
||
/*unsigned char lut[256];
|
||
double gamma = 0.5;
|
||
for (int i = 0; i < 256; i++)
|
||
{
|
||
lut[i] = cv::saturate_cast<uchar>(pow((float)i / 255.0, gamma) * 255.0f);
|
||
}
|
||
cv::MatIterator_<uchar> it = image.begin<uchar>();
|
||
cv::MatIterator_<uchar> end = image.end<uchar>();
|
||
while (it != end)
|
||
{
|
||
*it = lut[(*it)];
|
||
it++;
|
||
}*/
|
||
|
||
//cv::morphologyEx(image, image, cv::MORPH_CLOSE, cv::Mat(3, 3, CV_8U), cv::Point(-1, -1), 1);
|
||
cv::Mat cameraMatrix = cv::Mat(3, 3, CV_64FC1, cv::Scalar::all(0)); //摄像机内参数矩阵
|
||
cv::Mat distCoeffs = cv::Mat(1, 5, CV_64FC1, cv::Scalar::all(0)); //摄像机的5个畸变系数:k1,k2,p1,p2,k3
|
||
|
||
cameraMatrix.at<double>(0, 0) = m_CameraCalibrationCfg->m_CameraMatrix00->GetValue();
|
||
cameraMatrix.at<double>(0, 1) = m_CameraCalibrationCfg->m_CameraMatrix01->GetValue();
|
||
cameraMatrix.at<double>(0, 2) = m_CameraCalibrationCfg->m_CameraMatrix02->GetValue();
|
||
cameraMatrix.at<double>(1, 0) = m_CameraCalibrationCfg->m_CameraMatrix10->GetValue();
|
||
cameraMatrix.at<double>(1, 1) = m_CameraCalibrationCfg->m_CameraMatrix11->GetValue();
|
||
cameraMatrix.at<double>(1, 2) = m_CameraCalibrationCfg->m_CameraMatrix12->GetValue();
|
||
cameraMatrix.at<double>(2, 0) = m_CameraCalibrationCfg->m_CameraMatrix20->GetValue();
|
||
cameraMatrix.at<double>(2, 1) = m_CameraCalibrationCfg->m_CameraMatrix21->GetValue();
|
||
cameraMatrix.at<double>(2, 2) = m_CameraCalibrationCfg->m_CameraMatrix22->GetValue();
|
||
distCoeffs.at<double>(0, 0) = m_CameraCalibrationCfg->m_DistCoeffs0->GetValue();
|
||
distCoeffs.at<double>(0, 1) = m_CameraCalibrationCfg->m_DistCoeffs1->GetValue();
|
||
distCoeffs.at<double>(0, 2) = m_CameraCalibrationCfg->m_DistCoeffs2->GetValue();
|
||
distCoeffs.at<double>(0, 3) = m_CameraCalibrationCfg->m_DistCoeffs3->GetValue();
|
||
distCoeffs.at<double>(0, 4) = m_CameraCalibrationCfg->m_DistCoeffs4->GetValue();
|
||
|
||
|
||
|
||
cv::Mat calImage;
|
||
//cv::Mat mapx;
|
||
//cv::Mat mapy;;
|
||
//cv::initUndistortRectifyMap(cameraMatrix, distCoeffs, cv::Mat(), getOptimalNewCameraMatrix(cameraMatrix, distCoeffs, image.size(), 1, image.size(), 0), image.size(), CV_8U,mapx, mapy);
|
||
//cv::remap(image, calImage, mapx, mapy, cv::INTER_NEAREST);
|
||
undistort(image, calImage, cameraMatrix, distCoeffs);
|
||
//undistort(image, calImage, cameraMatrix, distCoeffs);
|
||
char buffer[123];
|
||
sprintf_s(buffer, sizeof(buffer), "mapx:%d %d \n", calImage.cols, calImage.rows);
|
||
g_log->TraceInfo(buffer);
|
||
|
||
return calImage;
|
||
}
|
||
|
||
cv::Mat Calibration::CalibrationWithCfg(cv::Mat image)
|
||
{
|
||
/*unsigned char lut[256];
|
||
double gamma = 0.5;
|
||
for (int i = 0; i < 256; i++)
|
||
{
|
||
lut[i] = cv::saturate_cast<uchar>(pow((float)i / 255.0, gamma) * 255.0f);
|
||
}
|
||
cv::MatIterator_<uchar> it = image.begin<uchar>();
|
||
cv::MatIterator_<uchar> end = image.end<uchar>();
|
||
while (it != end)
|
||
{
|
||
*it = lut[(*it)];
|
||
it++;
|
||
}*/
|
||
|
||
//cv::morphologyEx(image, image, cv::MORPH_CLOSE, cv::Mat(3, 3, CV_8U), cv::Point(-1, -1), 1);
|
||
cv::Mat cameraMatrix = cv::Mat(3, 3, CV_64FC1, cv::Scalar::all(0)); //摄像机内参数矩阵
|
||
cv::Mat distCoeffs = cv::Mat(1, 5, CV_64FC1, cv::Scalar::all(0)); //摄像机的5个畸变系数:k1,k2,p1,p2,k3
|
||
|
||
|
||
cameraMatrix.at<double>(0, 0) = m_CameraCalibrationCfg->m_CameraMatrix00->GetValue();
|
||
cameraMatrix.at<double>(0, 1) = m_CameraCalibrationCfg->m_CameraMatrix01->GetValue();
|
||
cameraMatrix.at<double>(0, 2) = m_CameraCalibrationCfg->m_CameraMatrix02->GetValue();
|
||
cameraMatrix.at<double>(1, 0) = m_CameraCalibrationCfg->m_CameraMatrix10->GetValue();
|
||
cameraMatrix.at<double>(1, 1) = m_CameraCalibrationCfg->m_CameraMatrix11->GetValue();
|
||
cameraMatrix.at<double>(1, 2) = m_CameraCalibrationCfg->m_CameraMatrix12->GetValue();
|
||
cameraMatrix.at<double>(2, 0) = m_CameraCalibrationCfg->m_CameraMatrix20->GetValue();
|
||
cameraMatrix.at<double>(2, 1) = m_CameraCalibrationCfg->m_CameraMatrix21->GetValue();
|
||
cameraMatrix.at<double>(2, 2) = m_CameraCalibrationCfg->m_CameraMatrix22->GetValue();
|
||
distCoeffs.at<double>(0, 0) = m_CameraCalibrationCfg->m_DistCoeffs0->GetValue();
|
||
distCoeffs.at<double>(0, 1) = m_CameraCalibrationCfg->m_DistCoeffs1->GetValue();
|
||
distCoeffs.at<double>(0, 2) = m_CameraCalibrationCfg->m_DistCoeffs2->GetValue();
|
||
distCoeffs.at<double>(0, 3) = m_CameraCalibrationCfg->m_DistCoeffs3->GetValue();
|
||
distCoeffs.at<double>(0, 4) = m_CameraCalibrationCfg->m_DistCoeffs4->GetValue();
|
||
cv::Mat calImage;
|
||
undistort(image, calImage, cameraMatrix, distCoeffs);
|
||
|
||
cv::Point2f srcTri[4];
|
||
srcTri[0] = cv::Point2f((float)m_CameraCalibrationCfg->m_ImageTopLeftX->GetValue(), (float)m_CameraCalibrationCfg->m_ImageTopLeftY->GetValue());
|
||
srcTri[1] = cv::Point2f((float)m_CameraCalibrationCfg->m_ImageTopRightX->GetValue(), (float)m_CameraCalibrationCfg->m_ImageTopRightY->GetValue());
|
||
srcTri[2] = cv::Point2f((float)m_CameraCalibrationCfg->m_ImageBottomRightX->GetValue(), (float)m_CameraCalibrationCfg->m_ImageBottomRightY->GetValue());
|
||
srcTri[3] = cv::Point2f((float)m_CameraCalibrationCfg->m_ImageBottomLeftX->GetValue(), (float)m_CameraCalibrationCfg->m_ImageBottomLeftY->GetValue());
|
||
|
||
cv::Point2f tempTri[4];
|
||
|
||
int tx = (m_CameraCalibrationCfg->m_PlatformTopRightX->GetValue() - m_CameraCalibrationCfg->m_PlatformTopLeftX->GetValue()) * m_CameraCalibrationCfg->m_MagnifyScale->GetValue();
|
||
int ty = (m_CameraCalibrationCfg->m_PlatformTopRightY->GetValue() - m_CameraCalibrationCfg->m_PlatformBottomRightY->GetValue()) * m_CameraCalibrationCfg->m_MagnifyScale->GetValue();
|
||
|
||
tempTri[0] = cv::Point2f(0.0f, 0.0f);
|
||
tempTri[1] = cv::Point2f((float)tx, 0.0f);
|
||
tempTri[2] = cv::Point2f((float)tx, (float)ty);
|
||
tempTri[3] = cv::Point2f(0.0f, (float)ty);
|
||
cv::Mat warp_mat = cv::getPerspectiveTransform(srcTri, tempTri);
|
||
cv::Mat tempImage(cv::Size(tx, ty), CV_8U);
|
||
warpPerspective(image, tempImage, warp_mat, tempImage.size());
|
||
return tempImage;
|
||
}
|
||
|
||
cv::Mat Calibration::CalibrationWithCfgBin(cv::Mat image)
|
||
{
|
||
/*unsigned char lut[256];
|
||
double gamma = 0.5;
|
||
for (int i = 0; i < 256; i++)
|
||
{
|
||
lut[i] = cv::saturate_cast<uchar>(pow((float)i / 255.0, gamma) * 255.0f);
|
||
}
|
||
cv::MatIterator_<uchar> it = image.begin<uchar>();
|
||
cv::MatIterator_<uchar> end = image.end<uchar>();
|
||
while (it != end)
|
||
{
|
||
*it = lut[(*it)];
|
||
it++;
|
||
}*/
|
||
|
||
//cv::morphologyEx(image, image, cv::MORPH_CLOSE, cv::Mat(3, 3, CV_8U), cv::Point(-1, -1), 1);
|
||
cv::Mat cameraMatrix = cv::Mat(3, 3, CV_64FC1, cv::Scalar::all(0)); //摄像机内参数矩阵
|
||
cv::Mat distCoeffs = cv::Mat(1, 5, CV_64FC1, cv::Scalar::all(0)); //摄像机的5个畸变系数:k1,k2,p1,p2,k3
|
||
|
||
|
||
cameraMatrix.at<double>(0, 0) = m_CameraCalibrationCfg->m_CameraMatrix00->GetValue();
|
||
cameraMatrix.at<double>(0, 1) = m_CameraCalibrationCfg->m_CameraMatrix01->GetValue();
|
||
cameraMatrix.at<double>(0, 2) = m_CameraCalibrationCfg->m_CameraMatrix02->GetValue();
|
||
cameraMatrix.at<double>(1, 0) = m_CameraCalibrationCfg->m_CameraMatrix10->GetValue();
|
||
cameraMatrix.at<double>(1, 1) = m_CameraCalibrationCfg->m_CameraMatrix11->GetValue();
|
||
cameraMatrix.at<double>(1, 2) = m_CameraCalibrationCfg->m_CameraMatrix12->GetValue();
|
||
cameraMatrix.at<double>(2, 0) = m_CameraCalibrationCfg->m_CameraMatrix20->GetValue();
|
||
cameraMatrix.at<double>(2, 1) = m_CameraCalibrationCfg->m_CameraMatrix21->GetValue();
|
||
cameraMatrix.at<double>(2, 2) = m_CameraCalibrationCfg->m_CameraMatrix22->GetValue();
|
||
distCoeffs.at<double>(0, 0) = m_CameraCalibrationCfg->m_DistCoeffs0->GetValue();
|
||
distCoeffs.at<double>(0, 1) = m_CameraCalibrationCfg->m_DistCoeffs1->GetValue();
|
||
distCoeffs.at<double>(0, 2) = m_CameraCalibrationCfg->m_DistCoeffs2->GetValue();
|
||
distCoeffs.at<double>(0, 3) = m_CameraCalibrationCfg->m_DistCoeffs3->GetValue();
|
||
distCoeffs.at<double>(0, 4) = m_CameraCalibrationCfg->m_DistCoeffs4->GetValue();
|
||
cv::Mat calImage;
|
||
undistort(image, calImage, cameraMatrix, distCoeffs);
|
||
|
||
cv::Point2f srcTri[4];
|
||
srcTri[0] = cv::Point2f((float)m_CameraCalibrationCfg->m_ImageTopLeftX->GetValue(), (float)m_CameraCalibrationCfg->m_ImageTopLeftY->GetValue());
|
||
srcTri[1] = cv::Point2f((float)m_CameraCalibrationCfg->m_ImageTopRightX->GetValue(), (float)m_CameraCalibrationCfg->m_ImageTopRightY->GetValue());
|
||
srcTri[2] = cv::Point2f((float)m_CameraCalibrationCfg->m_ImageBottomRightX->GetValue(), (float)m_CameraCalibrationCfg->m_ImageBottomRightY->GetValue());
|
||
srcTri[3] = cv::Point2f((float)m_CameraCalibrationCfg->m_ImageBottomLeftX->GetValue(), (float)m_CameraCalibrationCfg->m_ImageBottomLeftY->GetValue());
|
||
|
||
cv::Point2f tempTri[4];
|
||
|
||
int tx = (m_CameraCalibrationCfg->m_PlatformTopRightX->GetValue() - m_CameraCalibrationCfg->m_PlatformTopLeftX->GetValue()) * m_CameraCalibrationCfg->m_MagnifyScale->GetValue();
|
||
int ty = (m_CameraCalibrationCfg->m_PlatformTopRightY->GetValue() - m_CameraCalibrationCfg->m_PlatformBottomRightY->GetValue()) * m_CameraCalibrationCfg->m_MagnifyScale->GetValue();
|
||
|
||
tempTri[0] = cv::Point2f(0.0f, 0.0f);
|
||
tempTri[1] = cv::Point2f((float)tx, 0.0f);
|
||
tempTri[2] = cv::Point2f((float)tx, (float)ty);
|
||
tempTri[3] = cv::Point2f(0.0f, (float)ty);
|
||
cv::Mat warp_mat = cv::getPerspectiveTransform(srcTri, tempTri);
|
||
cv::Mat tempImage(cv::Size(tx, ty), CV_8U);
|
||
warpPerspective(calImage, tempImage, warp_mat, tempImage.size());
|
||
|
||
//cv::boxFilter(tempImage, tempImage, -1, cv::Size(3, 3));
|
||
cv::blur(tempImage, tempImage, cv::Size(3, 3));
|
||
if (m_CameraCalibrationCfg->m_BlackFace->GetValue()) {
|
||
cv::threshold(tempImage, tempImage, m_CameraCalibrationCfg->m_GrayRef->GetValue() - m_CameraCalibrationCfg->m_BinaryThresholdOffset->GetValue(), 255, cv::ThresholdTypes::THRESH_BINARY_INV);
|
||
}
|
||
else {
|
||
cv::threshold(tempImage, tempImage, m_CameraCalibrationCfg->m_GrayRef->GetValue() + m_CameraCalibrationCfg->m_BinaryThresholdOffset->GetValue(), 255, cv::ThresholdTypes::THRESH_BINARY);
|
||
}
|
||
//cv::Mat element = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(4, 4));
|
||
//morphologyEx(tempImage, tempImage, cv::MORPH_CLOSE, element);
|
||
return tempImage;
|
||
}
|
||
|
||
cv::Mat Calibration::CalibrationWithAssist(cv::Mat image)
|
||
{
|
||
cv::Mat cameraMatrix = cv::Mat(3, 3, CV_64FC1, cv::Scalar::all(0)); //摄像机内参数矩阵
|
||
cv::Mat distCoeffs = cv::Mat(1, 5, CV_64FC1, cv::Scalar::all(0)); //摄像机的5个畸变系数:k1,k2,p1,p2,k3
|
||
|
||
cameraMatrix.at<double>(0, 0) = m_CameraCalibrationCfg->m_CameraMatrix00->GetValue();
|
||
cameraMatrix.at<double>(0, 1) = m_CameraCalibrationCfg->m_CameraMatrix01->GetValue();
|
||
cameraMatrix.at<double>(0, 2) = m_CameraCalibrationCfg->m_CameraMatrix02->GetValue();
|
||
cameraMatrix.at<double>(1, 0) = m_CameraCalibrationCfg->m_CameraMatrix10->GetValue();
|
||
cameraMatrix.at<double>(1, 1) = m_CameraCalibrationCfg->m_CameraMatrix11->GetValue();
|
||
cameraMatrix.at<double>(1, 2) = m_CameraCalibrationCfg->m_CameraMatrix12->GetValue();
|
||
cameraMatrix.at<double>(2, 0) = m_CameraCalibrationCfg->m_CameraMatrix20->GetValue();
|
||
cameraMatrix.at<double>(2, 1) = m_CameraCalibrationCfg->m_CameraMatrix21->GetValue();
|
||
cameraMatrix.at<double>(2, 2) = m_CameraCalibrationCfg->m_CameraMatrix22->GetValue();
|
||
distCoeffs.at<double>(0, 0) = m_CameraCalibrationCfg->m_DistCoeffs0->GetValue();
|
||
distCoeffs.at<double>(0, 1) = m_CameraCalibrationCfg->m_DistCoeffs1->GetValue();
|
||
distCoeffs.at<double>(0, 2) = m_CameraCalibrationCfg->m_DistCoeffs2->GetValue();
|
||
distCoeffs.at<double>(0, 3) = m_CameraCalibrationCfg->m_DistCoeffs3->GetValue();
|
||
distCoeffs.at<double>(0, 4) = m_CameraCalibrationCfg->m_DistCoeffs4->GetValue();
|
||
cv::Mat calImage;
|
||
undistort(image, calImage, cameraMatrix, distCoeffs);
|
||
|
||
cv::Point2f srcTri[4];
|
||
srcTri[0] = cv::Point2f((float)m_CameraCalibrationCfg->m_ImageTopLeftX->GetValue(), (float)m_CameraCalibrationCfg->m_ImageTopLeftY->GetValue());
|
||
srcTri[1] = cv::Point2f((float)m_CameraCalibrationCfg->m_ImageTopRightX->GetValue(), (float)m_CameraCalibrationCfg->m_ImageTopRightY->GetValue());
|
||
srcTri[2] = cv::Point2f((float)m_CameraCalibrationCfg->m_ImageBottomRightX->GetValue(), (float)m_CameraCalibrationCfg->m_ImageBottomRightY->GetValue());
|
||
srcTri[3] = cv::Point2f((float)m_CameraCalibrationCfg->m_ImageBottomLeftX->GetValue(), (float)m_CameraCalibrationCfg->m_ImageBottomLeftY->GetValue());
|
||
|
||
cv::Point2f tempTri[4];
|
||
|
||
int tx = (m_CameraCalibrationCfg->m_PlatformTopRightX->GetValue() - m_CameraCalibrationCfg->m_PlatformTopLeftX->GetValue()) * m_CameraCalibrationCfg->m_MagnifyScale->GetValue();
|
||
int ty = (m_CameraCalibrationCfg->m_PlatformTopRightY->GetValue() - m_CameraCalibrationCfg->m_PlatformBottomRightY->GetValue()) * m_CameraCalibrationCfg->m_MagnifyScale->GetValue();
|
||
|
||
tempTri[0] = cv::Point2f(0.0f, 0.0f);
|
||
tempTri[1] = cv::Point2f((float)tx, 0.0f);
|
||
tempTri[2] = cv::Point2f((float)tx, (float)ty);
|
||
tempTri[3] = cv::Point2f(0.0f, (float)ty);
|
||
cv::Mat warp_mat = cv::getPerspectiveTransform(srcTri, tempTri);
|
||
cv::Mat wm(cv::Size(tx, ty), CV_8U);
|
||
warpPerspective(calImage, wm, warp_mat, wm.size());
|
||
|
||
|
||
cv::Mat tempImage;
|
||
cv::blur(wm, tempImage, cv::Size(3, 3));
|
||
if (m_CameraCalibrationCfg->m_ShowBinImage->GetValue()) {
|
||
if (m_CameraCalibrationCfg->m_BlackFace->GetValue()) {
|
||
cv::threshold(tempImage, tempImage, m_CameraCalibrationCfg->m_GrayRef->GetValue() - m_CameraCalibrationCfg->m_BinaryThresholdOffset->GetValue(), 255, cv::ThresholdTypes::THRESH_BINARY_INV);
|
||
}
|
||
else {
|
||
cv::threshold(tempImage, tempImage, m_CameraCalibrationCfg->m_GrayRef->GetValue() + m_CameraCalibrationCfg->m_BinaryThresholdOffset->GetValue(), 255, cv::ThresholdTypes::THRESH_BINARY);
|
||
}
|
||
}
|
||
else {
|
||
cv::Canny(tempImage, tempImage, m_CameraCalibrationCfg->m_GrayRef->GetValue(), m_CameraCalibrationCfg->m_GrayRef->GetValue() * 2, 3, true);
|
||
}
|
||
|
||
vector<vector<cv::Point>> contours;
|
||
vector<cv::Vec4i> hireachy;
|
||
cv::findContours(tempImage, contours, hireachy, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE, cv::Point());
|
||
for (size_t i = 0; i < contours.size(); i++) {
|
||
cv::Point2f P[4];
|
||
cv::RotatedRect rt = cv::minAreaRect(contours[i]);
|
||
rt.points(P);
|
||
float fv = 2.0f;
|
||
cv::Point2f fix[4] = { cv::Point2f(fv,-fv),cv::Point2f(fv,fv) ,cv::Point2f(-fv,fv) ,cv::Point2f(-fv,-fv) };
|
||
for (int fixindex = 0; fixindex < 4;fixindex++) {
|
||
P[fixindex] = P[fixindex] + fix[fixindex];
|
||
}
|
||
|
||
for (int j = 0; j <= 3; j++)
|
||
{
|
||
cv::line(wm, P[j], P[(j + 1) % 4], cv::Scalar(0, 0, 0), 1);
|
||
cv::circle(wm, rt.center, 1, cv::Scalar(0, 0, 0));
|
||
}
|
||
}
|
||
|
||
return wm;
|
||
}
|
||
|
||
cv::Mat Calibration::GetCornersImage(cv::Mat image) {
|
||
vector<cv::Point2f> corners;
|
||
cv::goodFeaturesToTrack(image, corners, 100, 0.01, 10, cv::Mat());
|
||
|
||
//char buffer[123];
|
||
for (auto t = 0; t < corners.size(); ++t) {
|
||
cv::circle(image, corners[t], 1, cv::Scalar(255), 1);
|
||
//sprintf_s(buffer, sizeof(buffer), "x:%.6f ,y:%.6f\n",corners[t].x,corners[t].y);
|
||
//OutputDebugString(buffer);
|
||
}
|
||
|
||
cv::TermCriteria criteria = cv::TermCriteria(cv::TermCriteria::EPS + cv::TermCriteria::MAX_ITER, 40, 0.001);
|
||
cornerSubPix(image, corners, cv::Size(5, 5), cv::Size(-1, -1), criteria);
|
||
/*for (auto t = 0; t < corners.size(); ++t) {
|
||
sprintf_s(buffer, sizeof(buffer), "x:%.6f ,y:%.6f\n", corners[t].x, corners[t].y);
|
||
OutputDebugString(buffer);
|
||
}*/
|
||
|
||
return image;
|
||
}
|
||
|
||
void Calibration::StartCalibrationCamera()
|
||
{
|
||
StopCalibrationCamera();
|
||
m_CalibrationCameraFlag = true;
|
||
m_CalibrationCameraThread = AtlCreateThread(CalibrationCameraProc, this);
|
||
}
|
||
|
||
void Calibration::StopCalibrationCamera()
|
||
{
|
||
m_CalibrationCameraFlag = false;
|
||
if (m_CalibrationCameraThread != INVALID_HANDLE_VALUE) {
|
||
if (WaitForSingleObject(m_CalibrationCameraThread, 1000) == WAIT_TIMEOUT) {
|
||
TerminateThread(m_CalibrationCameraThread, 1);
|
||
}
|
||
CloseHandle(m_CalibrationCameraThread);
|
||
m_CalibrationCameraThread = INVALID_HANDLE_VALUE;
|
||
}
|
||
}
|
||
|
||
DWORD WINAPI Calibration::CalibrationCameraProc(Calibration* _this)
|
||
{
|
||
if (_this) {
|
||
_this->CalibrationCameraRun();
|
||
}
|
||
return 0;
|
||
}
|
||
|
||
bool Calibration::CalibrationCameraRun()
|
||
{
|
||
SetCalibrationCameraProgress(0.0f);
|
||
SetCalibrationCameraInfo("");
|
||
|
||
if (m_CalibrationImages.empty()) {
|
||
SetCalibrationCameraInfo(g_LngManager->Toast_NoCalImage->ShowText());
|
||
return false;
|
||
}
|
||
char buffer[256];
|
||
cv::Size image_size; /* 图像的尺寸 */
|
||
image_size.width = m_CalibrationImages.front()->m_ImageMat.cols;
|
||
image_size.height = m_CalibrationImages.front()->m_ImageMat.rows;
|
||
float prostep = 0.9f / m_CalibrationImages.size();
|
||
cv::Size board_size = cv::Size(m_CameraCalibrationCfg->m_CalibrationHPoints->GetValue(), m_CameraCalibrationCfg->m_CalibrationVPoints->GetValue());
|
||
std::vector<std::vector<cv::Point2f>> image_points_seq; //保存检测到的所有角点
|
||
int calIndex = 0;
|
||
float prog = 0.0f;
|
||
for (auto imgmat : m_CalibrationImages) {
|
||
std::vector<cv::Point2f> image_points;
|
||
cv::Mat img;
|
||
cv::Mat src = imgmat->m_ImageMat;
|
||
//cv::Mat src = cv::imread("g:/Calibration/1.png");
|
||
try {
|
||
if (!cv::findChessboardCorners(src, board_size, image_points, 0)) {
|
||
sprintf_s(buffer, sizeof(buffer), u8"标定失败:图像%d存在异常", calIndex);
|
||
SetCalibrationCameraInfo(buffer);
|
||
return false;
|
||
}
|
||
else {
|
||
//cvtColor(src, img, CV_RGBA2GRAY);
|
||
src.copyTo(img);
|
||
cv::cornerSubPix(img, image_points, cv::Size(11, 11), cv::Size(-1, -1), cv::TermCriteria(CV_TERMCRIT_ITER + CV_TERMCRIT_EPS, 30, 0.01));
|
||
image_points_seq.push_back(image_points);
|
||
img.release();
|
||
}
|
||
calIndex++;
|
||
prog += prostep;
|
||
SetCalibrationCameraProgress(prog);
|
||
}
|
||
catch (cv::Exception& e) {
|
||
sprintf_s(buffer, sizeof(buffer), g_LngManager->Toast_CalImageFildException->ShowText(), calIndex);
|
||
SetCalibrationCameraInfo(buffer);
|
||
return false;
|
||
}
|
||
}
|
||
|
||
cv::Size square_size = cv::Size(m_CameraCalibrationCfg->m_CalibrationGridHSize->GetValue(), m_CameraCalibrationCfg->m_CalibrationGridVSize->GetValue());
|
||
std::vector<std::vector<cv::Point3f>> object_points; //保存标定板上角点的三维坐标
|
||
cv::Mat cameraMatrix = cv::Mat(3, 3, CV_64FC1, cv::Scalar::all(0)); //摄像机内参数矩阵
|
||
cv::Mat distCoeffs = cv::Mat(1, 5, CV_64FC1, cv::Scalar::all(0)); //摄像机的5个畸变系数:k1,k2,p1,p2,k3
|
||
std::vector<cv::Mat> tvecsMat; //每幅图像的旋转向量
|
||
std::vector<cv::Mat> rvecsMat; //每幅图像的平移向量
|
||
|
||
int total = image_points_seq.size();
|
||
|
||
for (size_t imageInxex = 0; imageInxex < m_CalibrationImages.size(); imageInxex++) {
|
||
std::vector<cv::Point3f> realPoint;
|
||
for (int i = 0; i < board_size.height; i++) {
|
||
for (int j = 0; j < board_size.width; j++) {
|
||
cv::Point3f tempPoint;
|
||
//假设标定板放在世界坐标系中z=0的平面上
|
||
tempPoint.x = j * square_size.width;
|
||
tempPoint.y = i * square_size.height;
|
||
//tempPoint.y = i * square_size.width;
|
||
//tempPoint.x = j * square_size.height;
|
||
tempPoint.z = 0;
|
||
realPoint.push_back(tempPoint);
|
||
}
|
||
}
|
||
object_points.push_back(realPoint);
|
||
}
|
||
SetCalibrationCameraProgress(0.95f);
|
||
double rel = cv::calibrateCamera(object_points, image_points_seq, image_size, cameraMatrix, distCoeffs, rvecsMat, tvecsMat, cv::CALIB_FIX_K3);
|
||
if (rel)
|
||
{
|
||
m_CameraCalibrationCfg->m_CameraMatrix00->SetValue( cameraMatrix.at<double>(0, 0));
|
||
m_CameraCalibrationCfg->m_CameraMatrix01->SetValue(cameraMatrix.at<double>(0, 1));
|
||
m_CameraCalibrationCfg->m_CameraMatrix02->SetValue(cameraMatrix.at<double>(0, 2));
|
||
m_CameraCalibrationCfg->m_CameraMatrix10->SetValue(cameraMatrix.at<double>(1, 0));
|
||
m_CameraCalibrationCfg->m_CameraMatrix11->SetValue(cameraMatrix.at<double>(1, 1));
|
||
m_CameraCalibrationCfg->m_CameraMatrix12->SetValue(cameraMatrix.at<double>(1, 2));
|
||
m_CameraCalibrationCfg->m_CameraMatrix20->SetValue(cameraMatrix.at<double>(2, 0));
|
||
m_CameraCalibrationCfg->m_CameraMatrix21->SetValue(cameraMatrix.at<double>(2, 1));
|
||
m_CameraCalibrationCfg->m_CameraMatrix22->SetValue(cameraMatrix.at<double>(2, 2));
|
||
m_CameraCalibrationCfg->m_DistCoeffs0->SetValue(distCoeffs.at<double>(0, 0));
|
||
m_CameraCalibrationCfg->m_DistCoeffs1->SetValue(distCoeffs.at<double>(0, 1));
|
||
m_CameraCalibrationCfg->m_DistCoeffs2->SetValue(distCoeffs.at<double>(0, 2));
|
||
m_CameraCalibrationCfg->m_DistCoeffs3->SetValue(distCoeffs.at<double>(0, 3));
|
||
m_CameraCalibrationCfg->m_DistCoeffs4->SetValue(distCoeffs.at<double>(0, 4));
|
||
ConfigManager::GetInstance()->SaveCameraCalibrationCfg();
|
||
|
||
string cfilePath = m_ImgPath + "Calibration.txt";
|
||
ofstream fout(cfilePath.c_str());
|
||
fout << cameraMatrix << endl;
|
||
fout << distCoeffs << endl;
|
||
fout.flush();
|
||
fout.close();
|
||
|
||
SetCalibrationCameraProgress(1.0f);
|
||
sprintf_s(buffer, sizeof(buffer), g_LngManager->Toast_CalImageSuccess->ShowText(), rel);
|
||
SetCalibrationCameraInfo(buffer);
|
||
|
||
/*std::vector<cv::Point2f> ips = image_points_seq[0];
|
||
std::vector<cv::Point3f> ops = object_points[0];
|
||
|
||
cv::Point2f srcTri[4];
|
||
srcTri[3] = cv::Point2f(ips[0].x, ips[0].y);
|
||
srcTri[2] = cv::Point2f(ips[25].x, ips[25].y);
|
||
srcTri[1] = cv::Point2f(ips[676].x, ips[676].y);
|
||
srcTri[0] = cv::Point2f(ips[701].x, ips[701].y);
|
||
|
||
cv::Point2f tempTri[4];
|
||
tempTri[0] = cv::Point2f(ops[0].x*10.0f, ops[0].y * 10.0f);
|
||
tempTri[1] = cv::Point2f(ops[25].x * 10.0f, ops[25].y * 10.0f);
|
||
tempTri[2] = cv::Point2f(ops[676].x * 10.0f, ops[676].y * 10.0f);
|
||
tempTri[3] = cv::Point2f(ops[701].x * 10.0f, ops[701].y * 10.0f);
|
||
|
||
|
||
m_WrapMat.release();
|
||
m_WrapMat = cv::getPerspectiveTransform(srcTri, tempTri);
|
||
cv::Mat tempImage(cv::Size(3200, 3200), CV_8U);
|
||
warpPerspective(m_CalibrationImages.front()->m_ImageMat, tempImage, m_WrapMat, tempImage.size());
|
||
cv::imwrite("g:/111.bmp", tempImage);*/
|
||
//cv::Mat calImage;
|
||
//undistort(m_CalibrationImages.front()->m_ImageMat, calImage, cameraMatrix, distCoeffs);
|
||
//cv::imwrite("g:/111.bmp", calImage);
|
||
return true;
|
||
}
|
||
else {
|
||
SetCalibrationCameraInfo(g_LngManager->Toast_CalImageFailTooLarge->ShowText());
|
||
return false;
|
||
}
|
||
}
|
||
|
||
bool Calibration::CalibrationTest()
|
||
{
|
||
vector<cv::Mat> imageInput;
|
||
char inputBuffer[50];
|
||
cv::Size image_size; /* 图像的尺寸 */
|
||
for (int i = 0; i < 5; i++) {
|
||
sprintf_s(inputBuffer, sizeof(inputBuffer), "g:/Calibration3/%d.bmp", i + 1);
|
||
cv::Mat srcTemp = cv::imread(inputBuffer);
|
||
imageInput.push_back(srcTemp);
|
||
if (i == 0) {
|
||
image_size.width = srcTemp.cols;
|
||
image_size.height = srcTemp.rows;
|
||
}
|
||
}
|
||
int rotate = -1;
|
||
switch (m_ExtCfg->m_ShowImageRotateAngle)
|
||
{
|
||
case ExtCfg::Angle_0: {
|
||
}break;
|
||
case ExtCfg::Angle_90: {
|
||
rotate = cv::ROTATE_90_COUNTERCLOCKWISE;
|
||
}break;
|
||
case ExtCfg::Angle_180: {
|
||
rotate = cv::ROTATE_180;
|
||
}break;
|
||
case ExtCfg::Angle_270: {
|
||
rotate = cv::ROTATE_90_CLOCKWISE;
|
||
}break;
|
||
}
|
||
|
||
if (rotate >= 0) {
|
||
for (size_t i = 0; i < imageInput.size(); i++) {
|
||
cv::rotate(imageInput[i], imageInput[i], rotate);
|
||
if (i == 0) {
|
||
image_size.width = imageInput[i].cols;
|
||
image_size.height = imageInput[i].rows;
|
||
}
|
||
}
|
||
}
|
||
cv::Size board_size = cv::Size(m_CameraCalibrationCfg->m_CalibrationHPoints->GetValue(), m_CameraCalibrationCfg->m_CalibrationVPoints->GetValue());
|
||
std::vector<std::vector<cv::Point2f>> image_points_seq; //保存检测到的所有角点
|
||
for (size_t imageInxex = 0; imageInxex < imageInput.size(); imageInxex++) {
|
||
std::vector<cv::Point2f> image_points;
|
||
cv::Mat img;
|
||
cv::Mat src = imageInput[imageInxex];
|
||
//cv::Mat src = cv::imread("g:/Calibration/1.png");
|
||
if (!cv::findChessboardCorners(src, board_size, image_points, 0)) {
|
||
return false;
|
||
}
|
||
else {
|
||
cv::cvtColor(src, img, CV_RGBA2GRAY);
|
||
sprintf_s(inputBuffer, sizeof(inputBuffer), "g:/Calibration3/%zd_gray.png", imageInxex + 1);
|
||
cv::imwrite(inputBuffer, img);
|
||
//find4QuadCornerSubpix(img, image_points, cv::Size(5, 5)); //对粗提取的角点进行精确化
|
||
|
||
cv::cornerSubPix(img, image_points, cv::Size(11, 11), cv::Size(-1, -1), cv::TermCriteria(CV_TERMCRIT_ITER + CV_TERMCRIT_EPS, 30, 0.01));
|
||
|
||
image_points_seq.push_back(image_points);
|
||
|
||
cv::Mat cb_corner;
|
||
cb_corner = src.clone();
|
||
drawChessboardCorners(cb_corner, board_size, image_points, true);
|
||
sprintf_s(inputBuffer, sizeof(inputBuffer), "g:/Calibration3/%d_corners.png", (int)imageInxex + 1);
|
||
cv::imwrite(inputBuffer, cb_corner);
|
||
}
|
||
}
|
||
|
||
cv::Size square_size = cv::Size(10, 10);
|
||
std::vector<std::vector<cv::Point3f>> object_points; //保存标定板上角点的三维坐标
|
||
cv::Mat cameraMatrix = cv::Mat(3, 3, CV_64FC1, cv::Scalar::all(0)); //摄像机内参数矩阵
|
||
cv::Mat distCoeffs = cv::Mat(1, 5, CV_64FC1, cv::Scalar::all(0)); //摄像机的5个畸变系数:k1,k2,p1,p2,k3
|
||
std::vector<cv::Mat> tvecsMat; //每幅图像的旋转向量
|
||
std::vector<cv::Mat> rvecsMat; //每幅图像的平移向量
|
||
|
||
int total = image_points_seq.size();
|
||
|
||
for (size_t imageInxex = 0; imageInxex < imageInput.size(); imageInxex++) {
|
||
std::vector<cv::Point3f> realPoint;
|
||
for (int i = 0; i < board_size.height; i++) {
|
||
for (int j = 0; j < board_size.width; j++) {
|
||
cv::Point3f tempPoint;
|
||
//假设标定板放在世界坐标系中z=0的平面上
|
||
tempPoint.x = i * square_size.width;
|
||
tempPoint.y = j * square_size.height;
|
||
tempPoint.z = 0;
|
||
realPoint.push_back(tempPoint);
|
||
}
|
||
}
|
||
object_points.push_back(realPoint);
|
||
}
|
||
|
||
double rel = cv::calibrateCamera(object_points, image_points_seq, image_size, cameraMatrix, distCoeffs, rvecsMat, tvecsMat, cv::CALIB_FIX_K3);
|
||
if (rel)
|
||
{
|
||
m_CameraCalibrationCfg->m_CameraMatrix00->SetValue(cameraMatrix.at<double>(0, 0));
|
||
m_CameraCalibrationCfg->m_CameraMatrix01->SetValue(cameraMatrix.at<double>(0, 1));
|
||
m_CameraCalibrationCfg->m_CameraMatrix02->SetValue(cameraMatrix.at<double>(0, 2));
|
||
m_CameraCalibrationCfg->m_CameraMatrix10->SetValue(cameraMatrix.at<double>(1, 0));
|
||
m_CameraCalibrationCfg->m_CameraMatrix11->SetValue(cameraMatrix.at<double>(1, 1));
|
||
m_CameraCalibrationCfg->m_CameraMatrix12->SetValue(cameraMatrix.at<double>(1, 2));
|
||
m_CameraCalibrationCfg->m_CameraMatrix20->SetValue(cameraMatrix.at<double>(2, 0));
|
||
m_CameraCalibrationCfg->m_CameraMatrix21->SetValue(cameraMatrix.at<double>(2, 1));
|
||
m_CameraCalibrationCfg->m_CameraMatrix22->SetValue(cameraMatrix.at<double>(2, 2));
|
||
m_CameraCalibrationCfg->m_DistCoeffs0->SetValue(distCoeffs.at<double>(0, 0));
|
||
m_CameraCalibrationCfg->m_DistCoeffs1->SetValue(distCoeffs.at<double>(0, 1));
|
||
m_CameraCalibrationCfg->m_DistCoeffs2->SetValue(distCoeffs.at<double>(0, 2));
|
||
m_CameraCalibrationCfg->m_DistCoeffs3->SetValue(distCoeffs.at<double>(0, 3));
|
||
m_CameraCalibrationCfg->m_DistCoeffs4->SetValue(distCoeffs.at<double>(0, 4));
|
||
ConfigManager::GetInstance()->SaveCameraCalibrationCfg();
|
||
|
||
ofstream fout("g:/Calibration3/Calibration.txt");
|
||
fout << cameraMatrix << endl;
|
||
fout << distCoeffs << endl;
|
||
fout.flush();
|
||
fout.close();
|
||
}
|
||
|
||
|
||
for (size_t imageInxex = 0; imageInxex < imageInput.size(); imageInxex++) {
|
||
cv::Mat src = imageInput[imageInxex];
|
||
cv::Mat cb_final;
|
||
undistort(src, cb_final, cameraMatrix, distCoeffs);
|
||
sprintf_s(inputBuffer, sizeof(inputBuffer), "g:/Calibration3/%zd_final.png", imageInxex + 1);
|
||
cv::imwrite(inputBuffer, cb_final);
|
||
}
|
||
|
||
cv::Mat tt;
|
||
cv::Mat srcTemp = cv::imread("g:/Calibration3/6.bmp");
|
||
cv::rotate(srcTemp, srcTemp, rotate);
|
||
undistort(srcTemp, tt, cameraMatrix, distCoeffs);
|
||
cv::imwrite("g:/Calibration3/6_final.png", tt);
|
||
return true;
|
||
}
|
||
|
||
void Calibration::Test()
|
||
{
|
||
cv::Point2f srcTri[4];
|
||
srcTri[0] = cv::Point2f(m_CameraCalibrationCfg->m_ImageTopLeftX->GetValue(), m_CameraCalibrationCfg->m_ImageTopLeftY->GetValue());
|
||
srcTri[1] = cv::Point2f(m_CameraCalibrationCfg->m_ImageTopRightX->GetValue(), m_CameraCalibrationCfg->m_ImageTopRightY->GetValue());
|
||
srcTri[2] = cv::Point2f(m_CameraCalibrationCfg->m_ImageBottomRightX->GetValue(), m_CameraCalibrationCfg->m_ImageBottomRightY->GetValue());
|
||
srcTri[3] = cv::Point2f(m_CameraCalibrationCfg->m_ImageBottomLeftX->GetValue(), m_CameraCalibrationCfg->m_ImageBottomLeftY->GetValue());
|
||
|
||
cv::Point2f tempTri[4];
|
||
// Mat warp_mat = getPerspectiveTransform(srcTri, dstTri);
|
||
/*tempTri[0] = cv::Point2f(m_CameraCalibrationCfg->m_PlatformTopLeftX, m_CameraCalibrationCfg->m_PlatformTopLeftY);
|
||
tempTri[1] = cv::Point2f(m_CameraCalibrationCfg->m_PlatformTopRightX, m_CameraCalibrationCfg->m_PlatformTopRightY);
|
||
tempTri[2] = cv::Point2f(m_CameraCalibrationCfg->m_PlatformBottomRightX, m_CameraCalibrationCfg->m_PlatformBottomRightY);
|
||
tempTri[3] = cv::Point2f(m_CameraCalibrationCfg->m_PlatformBottomLeftX, m_CameraCalibrationCfg->m_PlatformBottomLeftY);*/
|
||
int tlx = 0, tly = 0;
|
||
int tx = (m_CameraCalibrationCfg->m_PlatformTopRightX->GetValue() - m_CameraCalibrationCfg->m_PlatformTopLeftX->GetValue()) * 10;
|
||
int ty = (m_CameraCalibrationCfg->m_PlatformTopRightY->GetValue() - m_CameraCalibrationCfg->m_PlatformBottomRightY->GetValue()) * 10;
|
||
tempTri[0] = cv::Point2f(0, 0);
|
||
tempTri[1] = cv::Point2f(tx, 0);
|
||
tempTri[2] = cv::Point2f(tx, ty);
|
||
tempTri[3] = cv::Point2f(0, ty);
|
||
char inputBuffer[123];
|
||
vector<cv::Mat> imageInput;
|
||
for (int i = 0; i < 6; i++) {
|
||
sprintf_s(inputBuffer, sizeof(inputBuffer), "g:/Calibration3/%d_final.png", i + 1);
|
||
cv::Mat srcTemp = cv::imread(inputBuffer);
|
||
imageInput.push_back(srcTemp);
|
||
}
|
||
|
||
cv::Mat cameraMatrix = cv::Mat(3, 3, CV_64FC1, cv::Scalar::all(0)); //摄像机内参数矩阵
|
||
cv::Mat distCoeffs = cv::Mat(1, 5, CV_64FC1, cv::Scalar::all(0)); //摄像机的5个畸变系数:k1,k2,p1,p2,k3
|
||
try {
|
||
cameraMatrix.at<double>(0, 0) = m_CameraCalibrationCfg->m_CameraMatrix00->GetValue();
|
||
cameraMatrix.at<double>(0, 1) = m_CameraCalibrationCfg->m_CameraMatrix01->GetValue();
|
||
cameraMatrix.at<double>(0, 2) = m_CameraCalibrationCfg->m_CameraMatrix02->GetValue();
|
||
cameraMatrix.at<double>(1, 0) = m_CameraCalibrationCfg->m_CameraMatrix10->GetValue();
|
||
cameraMatrix.at<double>(1, 1) = m_CameraCalibrationCfg->m_CameraMatrix11->GetValue();
|
||
cameraMatrix.at<double>(1, 2) = m_CameraCalibrationCfg->m_CameraMatrix12->GetValue();
|
||
cameraMatrix.at<double>(2, 0) = m_CameraCalibrationCfg->m_CameraMatrix20->GetValue();
|
||
cameraMatrix.at<double>(2, 1) = m_CameraCalibrationCfg->m_CameraMatrix21->GetValue();
|
||
cameraMatrix.at<double>(2, 2) = m_CameraCalibrationCfg->m_CameraMatrix22->GetValue();
|
||
distCoeffs.at<double>(0, 0) = m_CameraCalibrationCfg->m_DistCoeffs0->GetValue();
|
||
distCoeffs.at<double>(0, 1) = m_CameraCalibrationCfg->m_DistCoeffs1->GetValue();
|
||
distCoeffs.at<double>(0, 2) = m_CameraCalibrationCfg->m_DistCoeffs2->GetValue();
|
||
distCoeffs.at<double>(0, 3) = m_CameraCalibrationCfg->m_DistCoeffs3->GetValue();
|
||
distCoeffs.at<double>(0, 4) = m_CameraCalibrationCfg->m_DistCoeffs4->GetValue();
|
||
}
|
||
catch (cv::Exception& e) {
|
||
OutputDebugString(e.msg.c_str());
|
||
}
|
||
|
||
/*for (size_t imageInxex = 0; imageInxex < imageInput.size(); imageInxex++) {
|
||
cv::Mat src = imageInput[imageInxex];
|
||
cv::Mat cb_final;
|
||
undistort(src, cb_final, cameraMatrix, distCoeffs);
|
||
sprintf_s(inputBuffer, sizeof(inputBuffer), "g:/Calibration3/%d_final.png", imageInxex);
|
||
cv::imwrite(inputBuffer, cb_final);
|
||
}*/
|
||
|
||
cv::Mat warp_mat = cv::getPerspectiveTransform(srcTri, tempTri);
|
||
|
||
//cv::Mat srcTemp = cv::imread("g:/Calibration2/5_final.png");
|
||
for (size_t i = 0; i < imageInput.size(); i++) {
|
||
cv::Mat imageTemp(cv::Size(tx, ty), CV_8U);
|
||
cv::warpPerspective(imageInput[i], imageTemp, warp_mat, imageTemp.size());
|
||
sprintf_s(inputBuffer, sizeof(inputBuffer), "g:/Calibration3/%zd_fix.png", i + 1);
|
||
cv::imwrite(inputBuffer, imageTemp);
|
||
}
|
||
|
||
}
|
||
|
||
void Calibration::Assist()
|
||
{
|
||
cv::Mat srcTemp = cv::imread("g:/Calibration3/12.bmp");
|
||
cv::Mat bg;
|
||
//cv::Scalar m = cv::mean(srcTemp);
|
||
//cv::divide(srcTemp, srcTemp, srcTemp, m.val[0]);
|
||
//cv::imwrite("g:/Calibration3/test_2.png", srcTemp);
|
||
cv::cvtColor(srcTemp, srcTemp, CV_RGBA2GRAY);
|
||
|
||
int rotate = -1;
|
||
switch (m_ExtCfg->m_ShowImageRotateAngle)
|
||
{
|
||
case ExtCfg::Angle_0: {
|
||
}break;
|
||
case ExtCfg::Angle_90: {
|
||
rotate = cv::ROTATE_90_COUNTERCLOCKWISE;
|
||
}break;
|
||
case ExtCfg::Angle_180: {
|
||
rotate = cv::ROTATE_180;
|
||
}break;
|
||
case ExtCfg::Angle_270: {
|
||
rotate = cv::ROTATE_90_CLOCKWISE;
|
||
}break;
|
||
}
|
||
if (rotate >= 0) {
|
||
cv::rotate(srcTemp, srcTemp, rotate);
|
||
}
|
||
|
||
//cv::blur(srcTemp, srcTemp, cv::Size(3, 3));
|
||
unsigned char lut[256];
|
||
double gamma = 0.5;
|
||
for (int i = 0; i < 256; i++)
|
||
{
|
||
lut[i] = cv::saturate_cast<uchar>(pow((float)i / 255.0, gamma) * 255.0f);
|
||
}
|
||
|
||
|
||
cv::MatIterator_<uchar> it = srcTemp.begin<uchar>();
|
||
cv::MatIterator_<uchar> end = srcTemp.end<uchar>();
|
||
while (it != end)
|
||
{
|
||
*it = lut[(*it)];
|
||
it++;
|
||
}
|
||
|
||
cv::morphologyEx(srcTemp, srcTemp, cv::MORPH_CLOSE, cv::Mat(3, 3, CV_8U), cv::Point(-1, -1), 1);
|
||
cv::imwrite("g:/Calibration3/test_blgam.png", srcTemp);
|
||
|
||
cv::Mat cameraMatrix = cv::Mat(3, 3, CV_64FC1, cv::Scalar::all(0)); //摄像机内参数矩阵
|
||
cv::Mat distCoeffs = cv::Mat(1, 5, CV_64FC1, cv::Scalar::all(0)); //摄像机的5个畸变系数:k1,k2,p1,p2,k3
|
||
|
||
cameraMatrix.at<double>(0, 0) = m_CameraCalibrationCfg->m_CameraMatrix00->GetValue();
|
||
cameraMatrix.at<double>(0, 1) = m_CameraCalibrationCfg->m_CameraMatrix01->GetValue();
|
||
cameraMatrix.at<double>(0, 2) = m_CameraCalibrationCfg->m_CameraMatrix02->GetValue();
|
||
cameraMatrix.at<double>(1, 0) = m_CameraCalibrationCfg->m_CameraMatrix10->GetValue();
|
||
cameraMatrix.at<double>(1, 1) = m_CameraCalibrationCfg->m_CameraMatrix11->GetValue();
|
||
cameraMatrix.at<double>(1, 2) = m_CameraCalibrationCfg->m_CameraMatrix12->GetValue();
|
||
cameraMatrix.at<double>(2, 0) = m_CameraCalibrationCfg->m_CameraMatrix20->GetValue();
|
||
cameraMatrix.at<double>(2, 1) = m_CameraCalibrationCfg->m_CameraMatrix21->GetValue();
|
||
cameraMatrix.at<double>(2, 2) = m_CameraCalibrationCfg->m_CameraMatrix22->GetValue();
|
||
distCoeffs.at<double>(0, 0) = m_CameraCalibrationCfg->m_DistCoeffs0->GetValue();
|
||
distCoeffs.at<double>(0, 1) = m_CameraCalibrationCfg->m_DistCoeffs1->GetValue();
|
||
distCoeffs.at<double>(0, 2) = m_CameraCalibrationCfg->m_DistCoeffs2->GetValue();
|
||
distCoeffs.at<double>(0, 3) = m_CameraCalibrationCfg->m_DistCoeffs3->GetValue();
|
||
distCoeffs.at<double>(0, 4) = m_CameraCalibrationCfg->m_DistCoeffs4->GetValue();
|
||
cv::Mat cb_final;
|
||
undistort(srcTemp, cb_final, cameraMatrix, distCoeffs);
|
||
cv::imwrite("g:/Calibration3/test_und.png", cb_final);
|
||
cv::Point2f srcTri[4];
|
||
srcTri[0] = cv::Point2f(m_CameraCalibrationCfg->m_ImageTopLeftX->GetValue(), m_CameraCalibrationCfg->m_ImageTopLeftY->GetValue());
|
||
srcTri[1] = cv::Point2f(m_CameraCalibrationCfg->m_ImageTopRightX->GetValue(), m_CameraCalibrationCfg->m_ImageTopRightY->GetValue());
|
||
srcTri[2] = cv::Point2f(m_CameraCalibrationCfg->m_ImageBottomRightX->GetValue(), m_CameraCalibrationCfg->m_ImageBottomRightY->GetValue());
|
||
srcTri[3] = cv::Point2f(m_CameraCalibrationCfg->m_ImageBottomLeftX->GetValue(), m_CameraCalibrationCfg->m_ImageBottomLeftY->GetValue());
|
||
|
||
cv::Point2f tempTri[4];
|
||
int tlx = 0, tly = 0;
|
||
int tx = (m_CameraCalibrationCfg->m_PlatformTopRightX->GetValue() - m_CameraCalibrationCfg->m_PlatformTopLeftX->GetValue()) * 10;
|
||
int ty = (m_CameraCalibrationCfg->m_PlatformTopRightY->GetValue() - m_CameraCalibrationCfg->m_PlatformBottomRightY->GetValue()) * 10;
|
||
tempTri[0] = cv::Point2f(0, 0);
|
||
tempTri[1] = cv::Point2f(tx, 0);
|
||
tempTri[2] = cv::Point2f(tx, ty);
|
||
tempTri[3] = cv::Point2f(0, ty);
|
||
cv::Mat warp_mat = cv::getPerspectiveTransform(srcTri, tempTri);
|
||
cv::Mat imageTemp(cv::Size(tx, ty), CV_8U);
|
||
warpPerspective(cb_final, imageTemp, warp_mat, imageTemp.size());
|
||
|
||
cv::imwrite("g:/Calibration3/test_wp_bg.png", imageTemp);
|
||
}
|
||
|
||
void Calibration::FindCountours()
|
||
{
|
||
/* cv::Mat srcTemp = cv::imread("g:/Calibration3/11.bmp");
|
||
cv::Mat srcTemp2 = cv::imread("g:/Calibration3/12.bmp");
|
||
cv::Mat bg;
|
||
cv::Scalar m = cv::mean(srcTemp);
|
||
cv::divide(srcTemp, srcTemp2, srcTemp, m.val[0]);
|
||
cv::imwrite("g:/Calibration3/test_2.png", srcTemp);
|
||
cv::cvtColor(srcTemp, srcTemp, CV_RGBA2GRAY);
|
||
|
||
int rotate = -1;
|
||
switch (m_ExtCfg->m_ShowImageRotateAngle)
|
||
{
|
||
case ExtCfg::Angle_0: {
|
||
}break;
|
||
case ExtCfg::Angle_90: {
|
||
rotate = cv::ROTATE_90_COUNTERCLOCKWISE;
|
||
}break;
|
||
case ExtCfg::Angle_180: {
|
||
rotate = cv::ROTATE_180;
|
||
}break;
|
||
case ExtCfg::Angle_270: {
|
||
rotate = cv::ROTATE_90_CLOCKWISE;
|
||
}break;
|
||
}
|
||
if (rotate >= 0) {
|
||
cv::rotate(srcTemp, srcTemp, rotate);
|
||
}
|
||
|
||
//cv::blur(srcTemp, srcTemp, cv::Size(3, 3));
|
||
unsigned char lut[256];
|
||
double gamma = 0.5;
|
||
for (int i = 0; i < 256; i++)
|
||
{
|
||
lut[i] = cv::saturate_cast<uchar>(pow((float)i / 255.0, gamma) * 255.0f);
|
||
}
|
||
|
||
|
||
cv::MatIterator_<uchar> it = srcTemp.begin<uchar>();
|
||
cv::MatIterator_<uchar> end = srcTemp.end<uchar>();
|
||
while (it != end)
|
||
{
|
||
*it = lut[(*it)];
|
||
it++;
|
||
}
|
||
|
||
cv::morphologyEx(srcTemp, srcTemp, cv::MORPH_CLOSE, cv::Mat(3, 3, CV_8U), cv::Point(-1, -1), 1);
|
||
cv::imwrite("g:/Calibration3/test_blgam.png", srcTemp);
|
||
|
||
cv::Mat cameraMatrix = cv::Mat(3, 3, CV_64FC1, cv::Scalar::all(0)); //摄像机内参数矩阵
|
||
cv::Mat distCoeffs = cv::Mat(1, 5, CV_64FC1, cv::Scalar::all(0)); //摄像机的5个畸变系数:k1,k2,p1,p2,k3
|
||
|
||
cameraMatrix.at<double>(0, 0) = m_CameraCalibrationCfg->m_CameraMatrix00;
|
||
cameraMatrix.at<double>(0, 1) = m_CameraCalibrationCfg->m_CameraMatrix01;
|
||
cameraMatrix.at<double>(0, 2) = m_CameraCalibrationCfg->m_CameraMatrix02;
|
||
cameraMatrix.at<double>(1, 0) = m_CameraCalibrationCfg->m_CameraMatrix10;
|
||
cameraMatrix.at<double>(1, 1) = m_CameraCalibrationCfg->m_CameraMatrix11;
|
||
cameraMatrix.at<double>(1, 2) = m_CameraCalibrationCfg->m_CameraMatrix12;
|
||
cameraMatrix.at<double>(2, 0) = m_CameraCalibrationCfg->m_CameraMatrix20;
|
||
cameraMatrix.at<double>(2, 1) = m_CameraCalibrationCfg->m_CameraMatrix21;
|
||
cameraMatrix.at<double>(2, 2) = m_CameraCalibrationCfg->m_CameraMatrix22;
|
||
distCoeffs.at<double>(0, 0) = m_CameraCalibrationCfg->m_DistCoeffs0;
|
||
distCoeffs.at<double>(0, 1) = m_CameraCalibrationCfg->m_DistCoeffs1;
|
||
distCoeffs.at<double>(0, 2) = m_CameraCalibrationCfg->m_DistCoeffs2;
|
||
distCoeffs.at<double>(0, 3) = m_CameraCalibrationCfg->m_DistCoeffs3;
|
||
distCoeffs.at<double>(0, 4) = m_CameraCalibrationCfg->m_DistCoeffs4;
|
||
cv::Mat cb_final;
|
||
undistort(srcTemp, cb_final, cameraMatrix, distCoeffs);
|
||
cv::imwrite("g:/Calibration3/test_und.png", cb_final);
|
||
cv::Point2f srcTri[4];
|
||
srcTri[0] = cv::Point2f(m_CameraCalibrationCfg->m_ImageTopLeftX, m_CameraCalibrationCfg->m_ImageTopLeftY);
|
||
srcTri[1] = cv::Point2f(m_CameraCalibrationCfg->m_ImageTopRightX, m_CameraCalibrationCfg->m_ImageTopRightY);
|
||
srcTri[2] = cv::Point2f(m_CameraCalibrationCfg->m_ImageBottomRightX, m_CameraCalibrationCfg->m_ImageBottomRightY);
|
||
srcTri[3] = cv::Point2f(m_CameraCalibrationCfg->m_ImageBottomLeftX, m_CameraCalibrationCfg->m_ImageBottomLeftY);
|
||
|
||
cv::Point2f tempTri[4];
|
||
int tlx = 0, tly = 0;
|
||
int tx = (m_CameraCalibrationCfg->m_PlatformTopRightX - m_CameraCalibrationCfg->m_PlatformTopLeftX) * 10;
|
||
int ty = (m_CameraCalibrationCfg->m_PlatformTopRightY - m_CameraCalibrationCfg->m_PlatformBottomRightY) * 10;
|
||
tempTri[0] = cv::Point2f(0, 0);
|
||
tempTri[1] = cv::Point2f(tx, 0);
|
||
tempTri[2] = cv::Point2f(tx, ty);
|
||
tempTri[3] = cv::Point2f(0, ty);
|
||
cv::Mat warp_mat = cv::getPerspectiveTransform(srcTri, tempTri);
|
||
cv::Mat imageTemp(cv::Size(tx, ty), CV_8U);
|
||
warpPerspective(cb_final, imageTemp, warp_mat, imageTemp.size());
|
||
|
||
cv::imwrite("g:/Calibration3/test_wp.png", imageTemp);
|
||
*/
|
||
/*cv::Mat imagebg = cv::imread("g:/Calibration3/test_wp_bg.png");
|
||
cv::Mat mat_mean, mat_stddev;
|
||
|
||
meanStdDev(imagebg, mat_mean, mat_stddev);
|
||
double m, s;
|
||
m = mat_mean.at<double>(0, 0);
|
||
s = mat_stddev.at<double>(0, 0);
|
||
|
||
cv::Mat imageTemp = cv::imread("g:/Calibration3/test_wp_2.png");
|
||
cv::resize(imageTemp, m_CalibrationSrc, m_CalibrationSrc.size());
|
||
m_CalibrationSrc.copyTo(m_CalibrationShow);
|
||
isDispUpdated = true;
|
||
|
||
cvtColor(imageTemp, imageTemp, CV_RGBA2GRAY);
|
||
boxFilter(imageTemp, imageTemp, -1, cv::Size(3, 3));
|
||
|
||
cv::Mat element = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(8, 8));
|
||
morphologyEx(imageTemp, imageTemp, cv::MORPH_CLOSE, element);
|
||
|
||
cv::threshold(imageTemp, imageTemp, m+10, 255, cv::ThresholdTypes::THRESH_BINARY);
|
||
//cv::Canny(imageTemp, imageTemp, m_CameraCalibrationCfg->m_BinaryThreshold, m_CameraCalibrationCfg->m_BinaryThreshold * 2, 3);
|
||
cv::imwrite("g:/Calibration3/test_can.png", imageTemp);
|
||
|
||
vector<vector<cv::Point>> contours;
|
||
vector<cv::Vec4i> hireachy;
|
||
|
||
//cv::findContours(imageTemp, contours, hireachy, cv::RETR_TREE, cv::CHAIN_APPROX_NONE, cv::Point());
|
||
cv::findContours(imageTemp, contours, hireachy, cv::RETR_LIST, cv::CHAIN_APPROX_SIMPLE, cv::Point());
|
||
cv::Mat imageTemp2(cv::Size(imageTemp.cols, imageTemp.rows), CV_8U, cv::Scalar::all(0));
|
||
cv::drawContours(imageTemp2, contours, -1, cv::Scalar(255), -1);
|
||
cv::imwrite("g:/Calibration3/test_ct.png", imageTemp2);
|
||
|
||
|
||
MetaData::Layer* layer = m_MetaData->GetLayer(0);
|
||
char buffer[256];
|
||
vector<MetaData::Part*> &partVec = m_MetaData->GetPartVec();
|
||
|
||
vector<cv::Mat> imageInput;
|
||
|
||
map<int, bool> hasMatch;
|
||
map<int, MatchInfo*> matchInfos;
|
||
|
||
for (auto dbm : layer->data_blocks_map) {
|
||
vector<MetaData::DataBlock*>* dbs = dbm.second;
|
||
MatchInfo* mi = new MatchInfo();
|
||
mi->m_PartId = dbm.first;
|
||
|
||
MetaData::Part* part = m_MetaData->GetPart(dbm.first);
|
||
float xSize = 0.0f, ySize = 0.0f;
|
||
xSize = 2000.0f;
|
||
ySize = 2000.0f;
|
||
cv::Mat imageTemp3(cv::Size(xSize, ySize), CV_8U, cv::Scalar::all(0));
|
||
|
||
cv::Mat imageTempMat(cv::Size(xSize, ySize), CV_8U, cv::Scalar::all(0));
|
||
vector<vector<cv::Point>> contoursTemp;
|
||
|
||
for (unsigned int dbIndex = 0; dbIndex < dbs->size(); ++dbIndex) {
|
||
MetaData::DataBlock* db = (*dbs)[dbIndex];
|
||
if (m_MetaData->m_FirstLayerBlockMap.find(db) == m_MetaData->m_FirstLayerBlockMap.end())continue;
|
||
BPBinary::BinDataBlock* pdb = m_MetaData->m_FirstLayerBlockMap[db];
|
||
if (!pdb)continue;
|
||
MetaData::ParameterSet* paramSet = m_MetaData->GetParameterSet(db->references->process);
|
||
if (!paramSet)continue;
|
||
if (pdb->type == BIN_VECTOR) {
|
||
for (unsigned int j = 0; j < pdb->point_indexs.size(); ++j) {
|
||
BPBinary::VectorPoint* pvp = (BPBinary::VectorPoint*)pdb->point_indexs[j];
|
||
float tempx1 = pvp->x1 * 10.0f + 1000.0f;
|
||
float tempy1 = 1000.0f - pvp->y1 * 10.0f;
|
||
float tempx2 = pvp->x2 * 10.0f + 1000.0f;
|
||
float tempy2 = 1000.0f - pvp->y2 * 10.0f;
|
||
cv::Point2f p1(tempx1, tempy1);
|
||
cv::Point2f p2(tempx2, tempy2);
|
||
cv::line(imageTemp3, p1, p2, cv::Scalar(255), 1);
|
||
}
|
||
}
|
||
else if (pdb->type == BIN_CHAIN) {
|
||
std::vector<cv::Point> line;
|
||
for (unsigned int j = 0; j < pdb->point_indexs.size(); ++j) {
|
||
BPBinary::ChainPoint* point = (BPBinary::ChainPoint*)pdb->point_indexs[j];
|
||
for (unsigned int k = 0; k < point->points.size(); ++k) {
|
||
float tempx = point->points[k]->x * 10.0f + 1000.0f;
|
||
float tempy = 1000.0f - point->points[k]->y * 10.0f;
|
||
cv::Point2f p(tempx, tempy);
|
||
line.push_back(p);
|
||
}
|
||
}
|
||
if (!line.empty()) {
|
||
//cv::polylines(imageTemp3, line,false, cv::Scalar(255));
|
||
for (size_t lindex = 0; lindex < line.size() - 1; lindex++) {
|
||
cv::line(imageTemp3, line[lindex], line[lindex + 1], cv::Scalar(255), 1);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
cv::Mat element2 = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(4, 4));
|
||
morphologyEx(imageTemp3, imageTemp3, cv::MORPH_CLOSE, element2);
|
||
imageTemp3.copyTo(mi->m_TplImage);
|
||
matchInfos[mi->m_PartId] = mi;
|
||
sprintf_s(buffer, sizeof(buffer), "g:/Calibration3/part%d-all.png", dbm.first);
|
||
cv::imwrite(buffer, imageTemp3);
|
||
}
|
||
|
||
for (auto dbm:layer->data_blocks_map) {
|
||
vector<MetaData::DataBlock*>* dbs = dbm.second;
|
||
MatchInfo* mi = matchInfos[dbm.first];
|
||
|
||
MetaData::Part* part = m_MetaData->GetPart(dbm.first);
|
||
float xMin = FLT_MAX,xMax=-FLT_MAX,yMin= FLT_MAX,yMax= -FLT_MAX,xSize=0.0f,ySize=0.0f;
|
||
xSize = 2000.0f;
|
||
ySize = 2000.0f;
|
||
cv::Mat imageTemp3(cv::Size(xSize, ySize), CV_8U, cv::Scalar::all(0));
|
||
vector<vector<cv::Point>> contoursTemp;
|
||
|
||
for (unsigned int dbIndex = 0; dbIndex < dbs->size(); ++dbIndex) {
|
||
MetaData::DataBlock* db = (*dbs)[dbIndex];
|
||
if (m_MetaData->m_FirstLayerBlockMap.find(db) == m_MetaData->m_FirstLayerBlockMap.end())continue;
|
||
BPBinary::BinDataBlock* pdb = m_MetaData->m_FirstLayerBlockMap[db];
|
||
if (!pdb)continue;
|
||
if (pdb->type != BIN_CHAIN)continue;
|
||
MetaData::ParameterSet* paramSet = m_MetaData->GetParameterSet(db->references->process);
|
||
if (!paramSet)continue;
|
||
if (paramSet->set_type == "InBorderAd")continue;
|
||
if (paramSet->set_type.find("InBorder") == paramSet->set_type.npos)continue;
|
||
//MetaData::Part* part= m_MetaData->GetPart(db->references->part);
|
||
std::vector<cv::Point> line;
|
||
for (unsigned int j = 0; j < pdb->point_indexs.size(); ++j) {
|
||
BPBinary::ChainPoint* point = (BPBinary::ChainPoint*)pdb->point_indexs[j];
|
||
for (unsigned int k = 0; k < point->points.size(); ++k) {
|
||
float tempx = point->points[k]->x * 10.0f+1000.0f;
|
||
float tempy =1000.0f- point->points[k]->y * 10.0f;
|
||
cv::Point2f p(tempx, tempy);
|
||
|
||
line.push_back(p);
|
||
if (tempx < xMin)xMin = tempx;
|
||
if (tempx > xMax)xMax = tempx;
|
||
if (tempy < yMin)yMin = tempy;
|
||
if (tempy > yMax)yMax = tempy;
|
||
}
|
||
}
|
||
mi->m_PartArea = abs(xMax - xMin) * abs(yMax - yMin);
|
||
|
||
contoursTemp.push_back(line);
|
||
|
||
}
|
||
|
||
|
||
|
||
mi->m_TplContours = contoursTemp[0];
|
||
cv::drawContours(imageTemp3, contoursTemp, -1, cv::Scalar(255), 1);
|
||
imageTemp3.copyTo(mi->m_TplContourImage);
|
||
|
||
imageInput.push_back(imageTemp3);
|
||
sprintf_s(buffer, sizeof(buffer), "g:/Calibration3/part%d.png",dbm.first );
|
||
cv::imwrite(buffer, imageTemp3);
|
||
}
|
||
|
||
|
||
|
||
for (size_t i = 0; i < contours.size(); i++) {
|
||
cv::Mat imageTempx(cv::Size(2000, 2000), CV_8U, cv::Scalar::all(0));
|
||
vector<vector<cv::Point>> contoursTemp;
|
||
contoursTemp.push_back(contours[i]);
|
||
cv::drawContours(imageTempx, contoursTemp, -1, cv::Scalar(255), 1);
|
||
sprintf_s(buffer, sizeof(buffer), "g:/Calibration3/cour/cour%d.png",i);
|
||
cv::imwrite(buffer, imageTempx);
|
||
}
|
||
|
||
|
||
for (auto mat : matchInfos) {
|
||
double minRel = FLT_MAX;
|
||
int minIndex = -1;
|
||
for (size_t i = 0; i < contours.size();i++) {
|
||
//cv::RotatedRect rr= cv::minAreaRect(ct);
|
||
cv::Rect rt = cv::boundingRect(contours[i]);
|
||
float area = rt.area();
|
||
|
||
if (mat.second->m_PartArea*0.9f > area)
|
||
{
|
||
continue;
|
||
}
|
||
|
||
double v = cv::matchShapes(mat.second->m_TplContours, contours[i], cv::CONTOURS_MATCH_I2, 0);
|
||
if (v < minRel)
|
||
{
|
||
if (hasMatch.find(i) == hasMatch.end()) {
|
||
minRel = v;
|
||
minIndex = i;
|
||
//hasMatch[mat.first] = true;
|
||
}
|
||
|
||
}
|
||
}
|
||
if (minIndex != -1)
|
||
{
|
||
cv::Mat imageTemp22(cv::Size(imageTemp.cols, imageTemp.rows), CV_8U, cv::Scalar::all(0));
|
||
//cv::drawContours(imageTemp22, contoursTemp, -1, cv::Scalar(255), cv::FILLED);
|
||
|
||
cv::Point2f center;
|
||
center.x = float(imageTemp.cols / 2.0 );
|
||
center.y = float(imageTemp.rows / 2.0);
|
||
cv::Mat M = cv::getRotationMatrix2D(center, 190, 1);
|
||
cv::warpAffine(mat.second->m_TplImage, imageTemp22, M, imageTemp22.size(), 1, cv::BORDER_CONSTANT, cv::Scalar::all(0));
|
||
// cv::rotate(mat.second->m_TplImage, imageTemp22, cv::ROTATE_90_COUNTERCLOCKWISE);
|
||
imageTemp22.copyTo(mat.second->m_MatchImage);
|
||
sprintf_s(buffer, sizeof(buffer), "g:/Calibration3/part%d_match_%d_all.png", mat.first, minIndex);
|
||
cv::imwrite(buffer, imageTemp22);
|
||
|
||
vector<vector<cv::Point>> contoursmat;
|
||
cv::findContours(imageTemp22, contoursmat, hireachy, cv::RETR_LIST, cv::CHAIN_APPROX_SIMPLE, cv::Point());
|
||
|
||
mat.second->m_MatchContours = contoursmat[0];
|
||
//mat.second->m_MatchContours = contours[minIndex];
|
||
hasMatch[minIndex] = true;
|
||
cv::Moments mom = cv::moments(mat.second->m_TplContours, true);
|
||
double cX = mom.m10 / mom.m00; double cY = mom.m01 / mom.m00;
|
||
float a1 = (float)(mom.m20 / mom.m00 - cX * cX);
|
||
float b1 = (float)(2 * (mom.m11 / mom.m00 - cX * cY));
|
||
float c1 = (float)(mom.m02 / mom.m00 - cY * cY);
|
||
float tanAngle1 = cv::fastAtan2(b1, a1 - c1) / 2.0f;
|
||
|
||
cv::Moments mom2 = cv::moments(mat.second->m_MatchContours, true);
|
||
double cX2 = mom2.m10 / mom2.m00; double cY2 = mom2.m01 / mom2.m00;
|
||
float a2 = (float)(mom2.m20 / mom2.m00 - cX2 * cX2);
|
||
float b2 = (float)(2.0 * (mom2.m11 / mom2.m00 - cX2 * cY2));
|
||
float c2 = (float)(mom2.m02 / mom2.m00 - cY2 * cY2);
|
||
float tanAngle2 = cv::fastAtan2(b2, a2 - c2) / 2.0f;
|
||
|
||
mat.second->m_AimAngle = tanAngle2 - tanAngle1;
|
||
|
||
mat.second->m_TplCenter = cv::Point(static_cast<float>(mom.m10 / mom.m00), static_cast<float>(mom.m01 / mom.m00));
|
||
mat.second->m_MatchCenter = cv::Point(static_cast<float>(mom2.m10 / mom2.m00), static_cast<float>(mom2.m01 / mom2.m00));
|
||
|
||
cv::Mat imageTempx(cv::Size(mat.second->m_TplImage.cols, mat.second->m_TplImage.rows), CV_8U, cv::Scalar::all(0));
|
||
|
||
//for (size_t lindex = 0; lindex < contours[minIndex].size() - 1; lindex++) {
|
||
// cv::line(imageTempx, contours[minIndex][lindex], contours[minIndex][lindex + 1], cv::Scalar(255), 1);
|
||
//}
|
||
vector<vector<cv::Point>> contoursTemp;
|
||
vector<vector<cv::Point>> contourstpl;
|
||
contoursTemp.push_back(mat.second->m_MatchContours);
|
||
contourstpl.push_back(mat.second->m_TplContours);
|
||
cv::drawContours(imageTempx, contoursTemp, -1, cv::Scalar(255), 1);
|
||
imageTempx.copyTo(mat.second->m_MatchContourImage);
|
||
|
||
cv::drawContours(imageTempx, contourstpl, -1, cv::Scalar(255), 1);
|
||
cv::circle(imageTempx, mat.second->m_TplCenter,2,cv::Scalar::all(255));
|
||
cv::circle(imageTempx, mat.second->m_MatchCenter, 2, cv::Scalar::all(255));
|
||
//cv::drawContours(imageTempx, contours[minIndex], -1, cv::Scalar(255), 1);
|
||
sprintf_s(buffer, sizeof(buffer), "g:/Calibration3/part%d_match_%d.png", mat.first, minIndex);
|
||
cv::imwrite(buffer, imageTempx);
|
||
|
||
|
||
}
|
||
|
||
}
|
||
|
||
|
||
//cv::Ptr<cv::FeatureDetector> detector = cv::ORB::create(500);
|
||
//cv::Ptr<cv::DescriptorExtractor> descriptor = cv::ORB::create();
|
||
//cv::Ptr<cv::FeatureDetector> detector = cv::GFTTDetector::create(500, 0.01, 20);
|
||
//cv::Ptr<cv::DescriptorExtractor> descriptor=cv::SiftDescriptorExtractor::create();
|
||
cv::Ptr<cv::FeatureDetector> detector = cv::SIFT::create(500);
|
||
//cv::Ptr<cv::DescriptorExtractor> descriptor = cv::SiftDescriptorExtractor::create();
|
||
for (auto mat : matchInfos) {
|
||
vector<cv::KeyPoint> tplKeyPoint;
|
||
vector<cv::KeyPoint> matchKeyPoint;
|
||
cv::Mat tplDesc;
|
||
cv::Mat matchDesc;
|
||
detector->detectAndCompute(mat.second->m_TplImage, cv::Mat(),tplKeyPoint, tplDesc);
|
||
detector->detectAndCompute(mat.second->m_MatchImage, cv::Mat(), matchKeyPoint, matchDesc);
|
||
//detector->detect(mat.second->m_TplImage, tplKeyPoint);
|
||
//detector->detect(mat.second->m_MatchImage, matchKeyPoint);
|
||
|
||
//descriptor->compute(mat.second->m_TplImage, tplKeyPoint, tplDesc);
|
||
//descriptor->compute(mat.second->m_MatchImage, matchKeyPoint, matchDesc);
|
||
|
||
//vector<cv::Mat> train_desc(1, tplDesc);
|
||
// cv::Ptr <cv::DescriptorMatcher > matcher = cv::makePtr<cv::FlannBasedMatcher>(cv::makePtr<cv::flann::LshIndexParams>(12, 20, 2));
|
||
cv::Ptr <cv::DescriptorMatcher > matcher = cv::BFMatcher::create();
|
||
//matcher->add(train_desc);
|
||
//matcher->train();
|
||
|
||
//vector<vector<cv::DMatch> > matchePoints;
|
||
vector<cv::DMatch> matchePoints;
|
||
vector<cv::DMatch> GoodMatchePoints;
|
||
matcher->match(matchDesc, tplDesc, matchePoints);
|
||
|
||
float distMax = -FLT_MAX;
|
||
for (size_t i = 0; i < matchePoints.size(); i++) {
|
||
if (matchePoints[i].distance > distMax) {
|
||
distMax = matchePoints[i].distance;
|
||
}
|
||
}
|
||
distMax = distMax*0.9f;
|
||
for (size_t i = 0; i < matchePoints.size(); i++) {
|
||
if (matchePoints[i].distance < distMax) {
|
||
GoodMatchePoints.push_back(matchePoints[i]);
|
||
}
|
||
}
|
||
|
||
|
||
if (!GoodMatchePoints.empty()) {
|
||
float x1 = tplKeyPoint[GoodMatchePoints[0].trainIdx].pt.x;
|
||
float y1 = tplKeyPoint[GoodMatchePoints[0].trainIdx].pt.y;
|
||
float x2 = mat.second->m_TplCenter.x;
|
||
float y2 = mat.second->m_TplCenter.y;
|
||
//float x2 = matchKeyPoint[GoodMatchePoints[0].queryIdx].pt.x;
|
||
//float y2 = matchKeyPoint[GoodMatchePoints[0].queryIdx].pt.y;
|
||
|
||
float k1 = (y2 - y1) / (x2 - x1);
|
||
float arcLength1 = atan(k1);
|
||
double current_angle1 = arcLength1 * 180.0f / CV_PI;
|
||
|
||
float x3 = matchKeyPoint[GoodMatchePoints[0].queryIdx].pt.x;
|
||
float y3 = matchKeyPoint[GoodMatchePoints[0].queryIdx].pt.y;
|
||
//float x3 = mat.second->m_TplCenter.x;
|
||
//float y3 = mat.second->m_TplCenter.y;
|
||
float x4 = mat.second->m_MatchCenter.x;
|
||
float y4 = mat.second->m_MatchCenter.y;
|
||
float k2 = (y4 - y3) / (x4 - x3);
|
||
float arcLength2 = atan(k2);
|
||
double current_angle2 = arcLength2 * 180.0f / CV_PI;
|
||
|
||
float ang = current_angle1 - current_angle2;
|
||
float ang2 = tplKeyPoint[GoodMatchePoints[0].trainIdx].angle - matchKeyPoint[GoodMatchePoints[0].queryIdx].angle;
|
||
|
||
map<int, vector<float>> angleMap;
|
||
int count = 0;
|
||
for (int i = 0; i < GoodMatchePoints.size(); i++)
|
||
{
|
||
int index = round( tplKeyPoint.at(GoodMatchePoints.at(i).trainIdx).angle - matchKeyPoint.at(GoodMatchePoints.at(i).queryIdx).angle );
|
||
float ang = tplKeyPoint.at(GoodMatchePoints.at(i).trainIdx).angle - matchKeyPoint.at(GoodMatchePoints.at(i).queryIdx).angle;
|
||
|
||
//fileo << kp2.at(matches.at(i).trainIdx).angle - kp1.at(matches.at(i).queryIdx).angle << endl;
|
||
|
||
index = index > 0 ? index : index + 360.0f;
|
||
ang = ang > 0 ? ang : ang + 360.0f;
|
||
angleMap[index].push_back(ang);
|
||
//cout << index << endl;
|
||
|
||
}
|
||
int maxindex = -1;
|
||
int maxcount = 0;
|
||
for (auto ind : angleMap) {
|
||
if ((int)ind.second.size() >= maxcount)
|
||
{
|
||
maxindex = ind.first;
|
||
maxcount = ind.second.size();
|
||
}
|
||
}
|
||
float sum = 0.0f;
|
||
if (angleMap.find(maxindex) != angleMap.end()) {
|
||
for (auto andi : angleMap[maxindex]) {
|
||
sum += andi;
|
||
}
|
||
}
|
||
float avg=sum / angleMap[maxindex].size();
|
||
avg = (avg - 360) < 0 ? avg : (avg - 360);
|
||
|
||
|
||
cv::Mat first_match;
|
||
drawMatches(mat.second->m_MatchImage, matchKeyPoint, mat.second->m_TplImage, tplKeyPoint, GoodMatchePoints, first_match);
|
||
|
||
cv::line(first_match, mat.second->m_TplCenter, mat.second->m_MatchCenter, cv::Scalar::all(255));
|
||
cv::circle(first_match, mat.second->m_TplCenter, 2, cv::Scalar::all(255));
|
||
sprintf_s(buffer, sizeof(buffer), "g:/Calibration3/part%d_mat-%.3f.png", mat.first,ang);
|
||
imwrite(buffer, first_match);
|
||
}
|
||
|
||
|
||
}
|
||
*/
|
||
|
||
}
|
||
/*
|
||
uint32_t Calibration::GetCalibrationImage()
|
||
{
|
||
uint32_t rel = 0;
|
||
EnterCriticalSection(&cab_cs);
|
||
if (isDispUpdated) {
|
||
isDispUpdated = false;
|
||
if (m_DispTex == 0) {
|
||
glGenTextures(1, &m_DispTex);
|
||
glBindTexture(GL_TEXTURE_2D, m_DispTex);
|
||
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 800, 800, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, m_CalibrationShow.data);
|
||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||
glBindTexture(GL_TEXTURE_2D, 0);
|
||
}
|
||
else {
|
||
glBindTexture(GL_TEXTURE_2D, m_DispTex);
|
||
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 800, 800, GL_LUMINANCE, GL_UNSIGNED_BYTE, m_CalibrationShow.data);
|
||
glBindTexture(GL_TEXTURE_2D, 0);
|
||
}
|
||
|
||
}
|
||
rel = m_DispTex;
|
||
LeaveCriticalSection(&cab_cs);
|
||
return rel;
|
||
}
|
||
|
||
*/
|
||
|
||
//void Calibration::DrawUI()
|
||
//{
|
||
// if (!m_GraftingShow) {
|
||
// return;
|
||
// }
|
||
// if (m_Light && m_Door) {
|
||
// if (m_Door->IsActive())m_Light->SetActive(true);
|
||
// }
|
||
// ImGui::Begin(g_LngManager->Menu_ImageDebug->ShowText(), &m_GraftingShow, ImGuiWindowFlags_NoDocking);
|
||
// m_Camera->SetDemandCatpure(true);
|
||
// ImGui::BeginTabBar("GraftingBar");
|
||
// if (ImGui::BeginTabItem(g_LngManager->UI_Calibration->ShowText())) {
|
||
// ImGui::BeginGroup();
|
||
// ImGui::BeginChild("imgfun", ImVec2(AutoSize::s200.calcValue, -1), true);
|
||
// if (ImGui::Button(g_LngManager->UI_TakeCalPicture->ShowText(), ImVec2(-1, 0))) {
|
||
//
|
||
// /*CalibrationShowInfo* info = new CalibrationShowInfo();
|
||
// info->m_ImageMat = cv::imread("e:/1658478451.bmp");
|
||
// cv::cvtColor(info->m_ImageMat, info->m_ImageMat, CV_RGB2GRAY);
|
||
// info->Init();
|
||
// m_CalibrationImages.push_back(info);*/
|
||
//
|
||
// unsigned char* imagetemp;
|
||
// int width=0, height=0,datasize=0;
|
||
// m_Camera->GetOriginSize(width, height, datasize);
|
||
// if (datasize > 0) {
|
||
// imagetemp = new unsigned char[datasize];
|
||
// if (m_Camera->GetRawImage(imagetemp))
|
||
// {
|
||
// cv::Mat image(height, width, CV_8U, imagetemp);
|
||
// int rotate = -1;
|
||
// switch (m_ExtCfg->m_ShowImageRotateAngle)
|
||
// {
|
||
// case ExtCfg::Angle_0: {
|
||
// }break;
|
||
// case ExtCfg::Angle_90: {
|
||
// rotate = cv::ROTATE_90_COUNTERCLOCKWISE;
|
||
// }break;
|
||
// case ExtCfg::Angle_180: {
|
||
// rotate = cv::ROTATE_180;
|
||
// }break;
|
||
// case ExtCfg::Angle_270: {
|
||
// rotate = cv::ROTATE_90_CLOCKWISE;
|
||
// }break;
|
||
// }
|
||
// if (rotate >= 0) {
|
||
// cv::rotate(image, image, rotate);
|
||
// }
|
||
// CalibrationShowInfo* info = new CalibrationShowInfo();
|
||
// info->Init(image, imagetemp);
|
||
// m_CalibrationImages.push_back(info);
|
||
// }
|
||
// else {
|
||
// delete[]imagetemp;
|
||
// }
|
||
// }
|
||
// }
|
||
//
|
||
// ImGui::BeginChild("calshowlist", ImVec2(-1, ImGui::GetFrameHeight() - AutoSize::s60.calcValue), true);
|
||
// ImGui::Columns(2, "calshowhead");
|
||
// uint32_t calIndex = 0;
|
||
// char calshowBuffer[100];
|
||
// CalibrationShowInfo* delInfo = nullptr;
|
||
// for (auto calshow : m_CalibrationImages) {
|
||
// sprintf_s(calshowBuffer, sizeof(calshowBuffer), "%d", calIndex);
|
||
// if (ImGui::Selectable(calshowBuffer, m_CalShowSelectIndex == calIndex)) {
|
||
// m_CalShowSelectIndex = calIndex;
|
||
// }
|
||
// ImGui::NextColumn();
|
||
// sprintf_s(calshowBuffer, sizeof(calshowBuffer), "calshowDel%d", calIndex);
|
||
// ImGui::PushID(calshowBuffer);
|
||
// if (ImGui::Button(g_LngManager->Sys_Delete->ShowText(), ImVec2(-1, 0))) {
|
||
// delInfo = calshow;
|
||
// }
|
||
// ImGui::PopID();
|
||
// ImGui::NextColumn();
|
||
// ImGui::Separator();
|
||
// calIndex++;
|
||
// }
|
||
// if (delInfo) {
|
||
// for (list<CalibrationShowInfo*>::iterator it = m_CalibrationImages.begin(); it != m_CalibrationImages.end();) {
|
||
// CalibrationShowInfo* temp = *it;
|
||
// if (temp == delInfo) {
|
||
// it = m_CalibrationImages.erase(it);
|
||
// delete temp;
|
||
// }
|
||
// else {
|
||
// it++;
|
||
// }
|
||
// }
|
||
// }
|
||
// ImGui::Columns(1);
|
||
// ImGui::EndChild();
|
||
// if (ImGui::Button(g_LngManager->UI_Calibration->ShowText(), ImVec2(-1, 0))) {
|
||
//
|
||
// if (m_CalibrationImages.empty()) {
|
||
// g_Toast->AddToast(new ToastBean(g_LngManager->Toast_CalImageCheck->ShowText(), 5000, Toast::COLOR_ORANGE));
|
||
// }
|
||
// else {
|
||
// ImGui::SetNextWindowPos(ImVec2(500, ImGui::GetWindowHeight() / 2));
|
||
// ImGui::OpenPopup(g_LngManager->UI_Calibrating->ShowText());
|
||
// StartCalibrationCamera();
|
||
// }
|
||
// }
|
||
// if (ImGui::BeginPopupModal(g_LngManager->UI_Calibrating->ShowText(), 0, ImGuiWindowFlags_AlwaysAutoResize)) {
|
||
// ImGui::ProgressBar(GetCalibrationCameraProgress(), ImVec2(AutoSize::s200.calcValue, 0));
|
||
// string info = GetCalibrationCameraInfo();
|
||
// if (info != "") {
|
||
// ImGui::Text(info.c_str());
|
||
// }
|
||
// if (ImGui::Button(g_LngManager->UI_Exit->ShowText(), ImVec2(AutoSize::s200.calcValue, 0)))
|
||
// {
|
||
// ImGui::CloseCurrentPopup();
|
||
// }
|
||
// ImGui::EndPopup();
|
||
// }
|
||
// ImGui::EndChild();
|
||
//
|
||
// ImGui::EndGroup();
|
||
//
|
||
// ImGui::SameLine();
|
||
//
|
||
// ImGui::BeginGroup();
|
||
// ImGui::BeginChild("calshow", ImVec2(-1, -1), true);
|
||
// ImGui::PushItemWidth(80);
|
||
// ImGui::InputScalar(g_LngManager->UI_CalibrationHPoints->ShowText(), ImGuiDataType_U32, &m_CameraCalibrationCfg->m_CalibrationHPoints, 0, 0, 0, ImGuiInputTextFlags_EnterReturnsTrue);
|
||
// ImGui::SameLine(0, 20);
|
||
// ImGui::InputScalar(g_LngManager->UI_CalibrationVPoints->ShowText(), ImGuiDataType_U32, &m_CameraCalibrationCfg->m_CalibrationVPoints, 0, 0, 0, ImGuiInputTextFlags_EnterReturnsTrue);
|
||
// ImGui::SameLine(0, 20);
|
||
// ImGui::InputScalar(g_LngManager->UI_CalibrationGridHSize->ShowText(), ImGuiDataType_U32, &m_CameraCalibrationCfg->m_CalibrationGridHSize, 0, 0, 0, ImGuiInputTextFlags_EnterReturnsTrue);
|
||
// ImGui::SameLine(0, 20);
|
||
// ImGui::InputScalar(g_LngManager->UI_CalibrationGridVSize->ShowText(), ImGuiDataType_U32, &m_CameraCalibrationCfg->m_CalibrationGridVSize, 0, 0, 0, ImGuiInputTextFlags_EnterReturnsTrue);
|
||
// ImGui::PopItemWidth();
|
||
// ImGui::SameLine(0, 20);
|
||
// if (ImGui::Button(g_LngManager->UI_TakeAndSave->ShowText()))
|
||
// {
|
||
// TackPhotoAndSave();
|
||
// }
|
||
//
|
||
// ImGui::BeginChild("calshow", ImVec2(-1, -1), true, ImGuiWindowFlags_HorizontalScrollbar);
|
||
// if (m_CalShowSelectIndex<m_CalibrationImages.size()) {
|
||
// uint32_t showIndex = 0;
|
||
// CalibrationShowInfo* show = nullptr;
|
||
// for (auto calshow : m_CalibrationImages) {
|
||
// if (m_CalShowSelectIndex == showIndex)
|
||
// {
|
||
// show = calshow;
|
||
// }
|
||
// showIndex++;
|
||
// }
|
||
// if (show) {
|
||
// ImGui::Image((ImTextureID)show->GetTex(), ImVec2(show->m_Width, show->m_High));
|
||
// }
|
||
// }
|
||
// ImGui::EndChild();
|
||
// ImGui::EndChild();
|
||
//
|
||
// ImGui::EndGroup();
|
||
//
|
||
// ImGui::EndTabItem();
|
||
// }
|
||
// if (ImGui::BeginTabItem(g_LngManager->UI_FixLocation->ShowText())) {
|
||
// if (ImGui::Button(g_LngManager->UI_TakePicture->ShowText())) {
|
||
//
|
||
// /*CalibrationShowInfo* info1 = new CalibrationShowInfo();
|
||
// cv::Mat test = cv::imread("G:/calibration4/1646726369.bmp");
|
||
//
|
||
// cv::cvtColor(test, test, CV_RGBA2GRAY);
|
||
// info1->m_ImageMat = CalibrationImage(test);
|
||
// //info1->m_ImageMat = test;
|
||
// info1->Init();
|
||
// m_LocationImage = info1;
|
||
// //cv::imwrite("g:/123.bmp", info1->m_ImageMat);
|
||
// */
|
||
// unsigned char* imagetemp;
|
||
// int width = 0, height = 0, datasize = 0;
|
||
// m_Camera->GetOriginSize(width, height, datasize);
|
||
// if (datasize > 0) {
|
||
// imagetemp = new unsigned char[datasize];
|
||
// if (m_Camera->GetRawImage(imagetemp))
|
||
// {
|
||
// cv::Mat image(height, width, CV_8U, imagetemp);
|
||
// int rotate = -1;
|
||
// switch (m_ExtCfg->m_ShowImageRotateAngle)
|
||
// {
|
||
// case ExtCfg::Angle_0: {
|
||
// }break;
|
||
// case ExtCfg::Angle_90: {
|
||
// rotate = cv::ROTATE_90_COUNTERCLOCKWISE;
|
||
// }break;
|
||
// case ExtCfg::Angle_180: {
|
||
// rotate = cv::ROTATE_180;
|
||
// }break;
|
||
// case ExtCfg::Angle_270: {
|
||
// rotate = cv::ROTATE_90_CLOCKWISE;
|
||
// }break;
|
||
// }
|
||
// if (rotate >= 0) {
|
||
// cv::rotate(image, image, rotate);
|
||
// }
|
||
// if (m_LocationImage) {
|
||
// delete m_LocationImage;
|
||
// m_LocationImage = NULL;
|
||
// }
|
||
//
|
||
// cv::Mat cImage = CalibrationImage(image);
|
||
// CalibrationShowInfo* info = new CalibrationShowInfo();
|
||
// info->Init(cImage, cImage.data);
|
||
// m_LocationImage = info;
|
||
//
|
||
// delete[] imagetemp;
|
||
// }
|
||
// else {
|
||
// delete[]imagetemp;
|
||
// }
|
||
// }
|
||
//
|
||
// }
|
||
//
|
||
// ImGui::PushItemWidth(AutoSize::s120.calcValue);
|
||
// ImGui::InputInt(g_LngManager->UI_ImageTopLeftX->ShowText(), &m_CameraCalibrationCfg->m_ImageTopLeftX, 0, 0, ImGuiInputTextFlags_EnterReturnsTrue);
|
||
// ImGui::SameLine();
|
||
// ImGui::InputInt(g_LngManager->UI_ImageTopLeftY->ShowText(), &m_CameraCalibrationCfg->m_ImageTopLeftY, 0, 0, ImGuiInputTextFlags_EnterReturnsTrue);
|
||
// ImGui::SameLine();
|
||
// ImGui::InputInt(g_LngManager->UI_PlatformTopLeftX->ShowText(), &m_CameraCalibrationCfg->m_PlatformTopLeftX, 0, 0, ImGuiInputTextFlags_EnterReturnsTrue);
|
||
// ImGui::SameLine();
|
||
// ImGui::InputInt(g_LngManager->UI_PlatformTopLeftY->ShowText(), &m_CameraCalibrationCfg->m_PlatformTopLeftY, 0, 0, ImGuiInputTextFlags_EnterReturnsTrue);
|
||
//
|
||
// ImGui::InputInt(g_LngManager->UI_ImageTopRightX->ShowText(), &m_CameraCalibrationCfg->m_ImageTopRightX, 0, 0, ImGuiInputTextFlags_EnterReturnsTrue);
|
||
// ImGui::SameLine();
|
||
// ImGui::InputInt(g_LngManager->UI_ImageTopRightY->ShowText(), &m_CameraCalibrationCfg->m_ImageTopRightY, 0, 0, ImGuiInputTextFlags_EnterReturnsTrue);
|
||
// ImGui::SameLine();
|
||
// ImGui::InputInt(g_LngManager->UI_PlatformTopRightX->ShowText(), &m_CameraCalibrationCfg->m_PlatformTopRightX, 0, 0, ImGuiInputTextFlags_EnterReturnsTrue);
|
||
// ImGui::SameLine();
|
||
// ImGui::InputInt(g_LngManager->UI_PlatformTopRightY->ShowText(), &m_CameraCalibrationCfg->m_PlatformTopRightY, 0, 0, ImGuiInputTextFlags_EnterReturnsTrue);
|
||
//
|
||
// ImGui::InputInt(g_LngManager->UI_ImageBottomRightX->ShowText(), &m_CameraCalibrationCfg->m_ImageBottomRightX, 0, 0, ImGuiInputTextFlags_EnterReturnsTrue);
|
||
// ImGui::SameLine();
|
||
// ImGui::InputInt(g_LngManager->UI_ImageBottomRightY->ShowText(), &m_CameraCalibrationCfg->m_ImageBottomRightY, 0, 0, ImGuiInputTextFlags_EnterReturnsTrue);
|
||
// ImGui::SameLine();
|
||
// ImGui::InputInt(g_LngManager->UI_PlatformBottomRightX->ShowText(), &m_CameraCalibrationCfg->m_PlatformBottomRightX, 0, 0, ImGuiInputTextFlags_EnterReturnsTrue);
|
||
// ImGui::SameLine();
|
||
// ImGui::InputInt(g_LngManager->UI_PlatformBottomRightY->ShowText(), &m_CameraCalibrationCfg->m_PlatformBottomRightY, 0, 0, ImGuiInputTextFlags_EnterReturnsTrue);
|
||
//
|
||
// ImGui::InputInt(g_LngManager->UI_ImageBottomLeftX->ShowText(), &m_CameraCalibrationCfg->m_ImageBottomLeftX, 0, 0, ImGuiInputTextFlags_EnterReturnsTrue);
|
||
// ImGui::SameLine();
|
||
// ImGui::InputInt(g_LngManager->UI_ImageBottomLeftY->ShowText(), &m_CameraCalibrationCfg->m_ImageBottomLeftY, 0, 0, ImGuiInputTextFlags_EnterReturnsTrue);
|
||
// ImGui::SameLine();
|
||
// ImGui::InputInt(g_LngManager->UI_PlatformBottomLeftX->ShowText(), &m_CameraCalibrationCfg->m_PlatformBottomLeftX, 0, 0, ImGuiInputTextFlags_EnterReturnsTrue);
|
||
// ImGui::SameLine();
|
||
// ImGui::InputInt(g_LngManager->UI_PlatformBottomLeftY->ShowText(), &m_CameraCalibrationCfg->m_PlatformBottomLeftY, 0, 0, ImGuiInputTextFlags_EnterReturnsTrue);
|
||
// ImGui::PopItemWidth();
|
||
// //ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f));
|
||
// ImGui::BeginChild("locShow", ImVec2(-1, -1), true, ImGuiWindowFlags_HorizontalScrollbar);
|
||
//
|
||
// if (m_LocationImage) {
|
||
// ImVec2 pos = ImGui::GetCursorScreenPos();
|
||
// ImGui::Image((ImTextureID)m_LocationImage->GetTex(), ImVec2(m_LocationImage->m_Width, m_LocationImage->m_High));
|
||
// ImGuiIO& io = ImGui::GetIO();
|
||
//
|
||
// if (ImGui::IsItemHovered())
|
||
// {
|
||
// m_LastMouRefImgPosX = io.MousePos.x - pos.x + 0.5f;
|
||
// m_LastMouRefImgPosY = io.MousePos.y - pos.y + 0.5f;
|
||
// float my_tex_w = m_LocationImage->m_Width;
|
||
// float my_tex_h = m_LocationImage->m_High;
|
||
//
|
||
// ImGui::BeginTooltip();
|
||
// float region_sz = 40.0f;
|
||
// float region_x = io.MousePos.x - pos.x - region_sz * 0.5f; if (region_x < 0.0f) region_x = 0.0f; else if (region_x > my_tex_w - region_sz) region_x = my_tex_w - region_sz;
|
||
// float region_y = io.MousePos.y - pos.y - region_sz * 0.5f; if (region_y < 0.0f) region_y = 0.0f; else if (region_y > my_tex_h - region_sz) region_y = my_tex_h - region_sz;
|
||
//
|
||
// float zoom = 10.0f;
|
||
// ImGui::Text("Min: (%.2f, %.2f)", region_x, region_y);
|
||
// ImGui::Text("Max: (%.2f, %.2f)", region_x + region_sz, region_y + region_sz);
|
||
// ImGui::Text("Pos: (%d, %d)", m_LastMouRefImgPosX, m_LastMouRefImgPosY);
|
||
// ImVec2 uv0 = ImVec2((region_x) / my_tex_w, (region_y) / my_tex_h);
|
||
// ImVec2 uv1 = ImVec2((region_x + region_sz) / my_tex_w, (region_y + region_sz) / my_tex_h);
|
||
// //
|
||
// ImGui::GetWindowDrawList();
|
||
// //draw_list->AddImage((ImTextureID)m_LocationImage->GetTex(), ImVec2(0, 0), ImVec2(region_sz * zoom, region_sz * zoom), uv0, uv1, ImColor(255, 255, 255, 255));
|
||
// ImGui::ImageCross((ImTextureID)m_LocationImage->GetTex(), ImVec2(region_sz * zoom, region_sz * zoom), uv0, uv1, ImColor(255, 255, 255, 255), ImColor(255, 255, 255, 128), ImVec2((m_LastMouRefImgPosX - region_x) * 10, (m_LastMouRefImgPosY - region_y) * 10));
|
||
// ImGui::EndTooltip();
|
||
// }
|
||
// bool isclose = false;
|
||
// if (ImGui::BeginPopupContextItem("SetPoint"))
|
||
// {
|
||
// if (ImGui::Button(g_LngManager->UI_TopLeft->ShowText(), ImVec2(80, 0))) {
|
||
// m_CameraCalibrationCfg->m_ImageTopLeftX = m_LastMouRefImgPosX;
|
||
// m_CameraCalibrationCfg->m_ImageTopLeftY = m_LastMouRefImgPosY;
|
||
// ImGui::CloseCurrentPopup();
|
||
// }
|
||
// if (ImGui::Button(g_LngManager->UI_TopRight->ShowText(), ImVec2(80, 0))) {
|
||
// m_CameraCalibrationCfg->m_ImageTopRightX = m_LastMouRefImgPosX;
|
||
// m_CameraCalibrationCfg->m_ImageTopRightY = m_LastMouRefImgPosY;
|
||
// ImGui::CloseCurrentPopup();
|
||
// }
|
||
// if (ImGui::Button(g_LngManager->UI_BottomRight->ShowText(), ImVec2(80, 0))) {
|
||
// m_CameraCalibrationCfg->m_ImageBottomRightX = m_LastMouRefImgPosX;
|
||
// m_CameraCalibrationCfg->m_ImageBottomRightY = m_LastMouRefImgPosY;
|
||
// ImGui::CloseCurrentPopup();
|
||
// }
|
||
// if (ImGui::Button(g_LngManager->UI_BottomLeft->ShowText(), ImVec2(80, 0))) {
|
||
// m_CameraCalibrationCfg->m_ImageBottomLeftX = m_LastMouRefImgPosX;
|
||
// m_CameraCalibrationCfg->m_ImageBottomLeftY = m_LastMouRefImgPosY;
|
||
// ImGui::CloseCurrentPopup();
|
||
// }
|
||
// if (ImGui::Button(g_LngManager->UI_Exit->ShowText(), ImVec2(80, 0))) {
|
||
// ImGui::CloseCurrentPopup();
|
||
// }
|
||
// ImGui::EndPopup();
|
||
// }
|
||
//
|
||
// }
|
||
// ImGui::EndChild();
|
||
// // ImGui::PopStyleVar();
|
||
// ImGui::EndTabItem();
|
||
// }
|
||
// if (ImGui::BeginTabItem(g_LngManager->UI_Graft->ShowText())) {
|
||
// if (ImGui::Button(g_LngManager->UI_TakeBaseImage->ShowText()))
|
||
// {
|
||
// unsigned char* imagetemp;
|
||
// int width = 0, height = 0, datasize = 0;
|
||
// m_Camera->GetOriginSize(width, height, datasize);
|
||
// if (datasize > 0) {
|
||
// imagetemp = new unsigned char[datasize];
|
||
// if (m_Camera->GetRawImage(imagetemp))
|
||
// {
|
||
// cv::Mat image(height, width, CV_8U, imagetemp);
|
||
// int rotate = -1;
|
||
// switch (m_ExtCfg->m_ShowImageRotateAngle)
|
||
// {
|
||
// case ExtCfg::Angle_0: {
|
||
// }break;
|
||
// case ExtCfg::Angle_90: {
|
||
// rotate = cv::ROTATE_90_COUNTERCLOCKWISE;
|
||
// }break;
|
||
// case ExtCfg::Angle_180: {
|
||
// rotate = cv::ROTATE_180;
|
||
// }break;
|
||
// case ExtCfg::Angle_270: {
|
||
// rotate = cv::ROTATE_90_CLOCKWISE;
|
||
// }break;
|
||
// }
|
||
// if (rotate >= 0) {
|
||
// cv::rotate(image, image, rotate);
|
||
// }
|
||
//
|
||
// if (m_CoverImage) {
|
||
// delete m_CoverImage;
|
||
// m_CoverImage = NULL;
|
||
// }
|
||
// CalibrationShowInfo* info = new CalibrationShowInfo();
|
||
// info->Init(image, imagetemp);
|
||
// m_CoverImage = info;
|
||
// cv::imwrite(m_CoverImgPath, m_CoverImage->m_ImageMat);
|
||
// }
|
||
// else {
|
||
// delete[] imagetemp;
|
||
// }
|
||
// }
|
||
//
|
||
// }
|
||
// ImGui::SameLine();
|
||
// if (ImGui::Button(g_LngManager->UI_ShowBaseImage->ShowText()))
|
||
// {
|
||
// if (!m_CoverImage) {
|
||
// g_Toast->AddToast(new ToastBean(g_LngManager->Toast_BaseImageCheck->ShowText(), 3000, Toast::COLOR_RED));
|
||
// }
|
||
// else {
|
||
// ImGui::OpenPopup("ShowCoverImage");
|
||
// }
|
||
// }
|
||
// if (ImGui::BeginPopup("ShowCoverImage")) {
|
||
//
|
||
// ImGui::Image((ImTextureID)m_CoverImage->GetTex(), ImVec2(m_CoverImage->m_Width / 4, m_CoverImage->m_High / 4));
|
||
// if (ImGui::Button(g_LngManager->UI_Exit->ShowText(), ImVec2(-1, 0)))
|
||
// {
|
||
// ImGui::CloseCurrentPopup();
|
||
// }
|
||
// ImGui::EndPopup();
|
||
// }
|
||
// ImGui::SameLine(0, 50.0f);
|
||
//
|
||
// if (ImGui::Button(g_LngManager->UI_TakePlatformImage->ShowText()))
|
||
// {
|
||
// if (m_CameraCalibrationCfg->m_CoverImageJoin && !m_CoverImage) {
|
||
// g_Toast->AddToast(new ToastBean(g_LngManager->Toast_BaseImageCheck->ShowText(), 3000, Toast::COLOR_RED));
|
||
// }
|
||
// else {
|
||
// if (g_isDebug) {
|
||
// m_GraftShowImage->m_ImageMat.release();
|
||
// m_GraftMat.release();
|
||
// if (m_BasePartImage) {
|
||
// delete m_BasePartImage;
|
||
// m_BasePartImage = NULL;
|
||
// }
|
||
// CalibrationShowInfo* info = new CalibrationShowInfo();
|
||
// info->m_ImageMat = cv::imread("G:/TestGraf/3.bmp");
|
||
// cv::cvtColor(info->m_ImageMat, info->m_ImageMat, CV_RGB2GRAY);
|
||
// m_BasePartImage = info;
|
||
//
|
||
//
|
||
// if (m_CameraCalibrationCfg->m_CoverImageJoin && m_CoverImage) {
|
||
// cv::Mat divImage;
|
||
// cv::Scalar partMean = cv::mean(m_BasePartImage->m_ImageMat);
|
||
// cv::divide(m_BasePartImage->m_ImageMat, m_CoverImage->m_ImageMat, divImage, partMean.val[0]);
|
||
// if (m_CameraCalibrationCfg->m_ShowAssist) {
|
||
// m_GraftMat = CalibrationWithAssist(divImage);
|
||
// }
|
||
// else {
|
||
// m_GraftMat = CalibrationWithCfg(divImage);
|
||
// }
|
||
// }
|
||
// else {
|
||
// if (m_CameraCalibrationCfg->m_ShowAssist) {
|
||
// m_GraftMat = CalibrationWithAssist(m_BasePartImage->m_ImageMat);
|
||
// }
|
||
// else {
|
||
// m_GraftMat = CalibrationWithCfg(m_BasePartImage->m_ImageMat);
|
||
// }
|
||
// }
|
||
//
|
||
//
|
||
// m_GraftMat.copyTo(m_GraftShowImage->m_ImageMat);
|
||
// //m_GraftShowImage->m_ImageMat=cv::imread("G:/calibration4/1646813796.bmp");
|
||
// cv::cvtColor(m_GraftShowImage->m_ImageMat, m_GraftShowImage->m_ImageMat, cv::COLOR_GRAY2RGB);
|
||
//
|
||
// m_GraftShowImage->m_IsColor = true;
|
||
// m_ManualGraftSelectPart = 0;
|
||
// EnterCriticalSection(&m_Jc->m_cs);
|
||
// if (m_Jc->GetJob()) {
|
||
// int xLength = (m_CameraCalibrationCfg->m_PlatformTopRightX - m_CameraCalibrationCfg->m_PlatformTopLeftX) * m_CameraCalibrationCfg->m_MagnifyScale;
|
||
// int yLength = (m_CameraCalibrationCfg->m_PlatformTopRightY - m_CameraCalibrationCfg->m_PlatformBottomRightY) * m_CameraCalibrationCfg->m_MagnifyScale;
|
||
// int centerx = xLength / 2;
|
||
// int centery = yLength / 2;
|
||
// FileProcessor* jfp = m_Jc->GetJob();
|
||
// MetaData::Layer* player = jfp->GetLayer(0);
|
||
//
|
||
//
|
||
// for (unsigned int dbIndex = 0; dbIndex < player->data_blocks.size(); ++dbIndex) {
|
||
// MetaData::DataBlock* db = player->data_blocks[dbIndex];
|
||
// if (jfp->GetMetaData()->m_FirstLayerBlockMap.find(db) == jfp->GetMetaData()->m_FirstLayerBlockMap.end())continue;
|
||
// BPBinary::BinDataBlock* pdb = jfp->GetMetaData()->m_FirstLayerBlockMap[db];
|
||
// if (!pdb)continue;
|
||
// if (pdb->type != BIN_CHAIN)continue;
|
||
//
|
||
// MetaData::Part* cpart = jfp->GetMetaData()->GetPart(db->references->part);
|
||
//
|
||
// for (unsigned int j = 0; j < pdb->point_indexs.size(); ++j) {
|
||
// BPBinary::ChainPoint* point = (BPBinary::ChainPoint*)pdb->point_indexs[j];
|
||
// std::vector<cv::Point2f> line;
|
||
// for (unsigned int k = 0; k < point->points.size(); ++k) {
|
||
// double real_x = point->points[k]->x;
|
||
// double real_y = point->points[k]->y;
|
||
// double mov_x1 = cpart->partPosBean.m_XOffset + point->points[k]->x;
|
||
// double mov_y1 = cpart->partPosBean.m_YOffset + point->points[k]->y;
|
||
// real_x = (mov_x1 - cpart->partPosBean.m_PartCenterX) * cos(cpart->partPosBean.m_Radians) - (mov_y1 - cpart->partPosBean.m_PartCenterY) * sin(cpart->partPosBean.m_Radians) + cpart->partPosBean.m_PartCenterX;
|
||
// real_y = (mov_x1 - cpart->partPosBean.m_PartCenterX) * sin(cpart->partPosBean.m_Radians) + (mov_y1 - cpart->partPosBean.m_PartCenterY) * cos(cpart->partPosBean.m_Radians) + cpart->partPosBean.m_PartCenterY;
|
||
//
|
||
// float tempx = real_x * m_CameraCalibrationCfg->m_MagnifyScale + centerx;
|
||
// float tempy = centery - real_y * m_CameraCalibrationCfg->m_MagnifyScale;
|
||
// cv::Point2f p(tempx, tempy);
|
||
// line.push_back(p);
|
||
//
|
||
// }
|
||
// int cc = j % 10;
|
||
// if (!line.empty()) {
|
||
// if (m_CameraCalibrationCfg->m_ShowAssist) {
|
||
// cv::RotatedRect re = cv::minAreaRect(line);
|
||
//
|
||
// cv::Point2f P[4];
|
||
// re.points(P);
|
||
// for (int j = 0; j <= 3; j++)
|
||
// {
|
||
// cv::line(m_GraftShowImage->m_ImageMat, P[j], P[(j + 1) % 4], m_ColorMap[cc], 1);
|
||
// cv::circle(m_GraftShowImage->m_ImageMat, re.center, 1, m_ColorMap[cc]);
|
||
// }
|
||
// }
|
||
// int pcx = cpart->partPosBean.m_PartCenterX * m_CameraCalibrationCfg->m_MagnifyScale + centerx + 0.5;
|
||
// int pcy = centery - cpart->partPosBean.m_PartCenterY * m_CameraCalibrationCfg->m_MagnifyScale + 0.5;
|
||
// for (size_t lindex = 0; lindex < line.size() - 1; lindex++) {
|
||
// if (m_ManualGraftSelectPart == cpart->id) {
|
||
// cv::line(m_GraftShowImage->m_ImageMat, line[lindex], line[lindex + 1], cv::Scalar(0, 255, 0), 1);
|
||
// }
|
||
// else {
|
||
// cv::line(m_GraftShowImage->m_ImageMat, line[lindex], line[lindex + 1], cv::Scalar(255, 0, 0), 1);
|
||
// }
|
||
// }
|
||
// //cv::circle(m_GraftShowImage->m_ImageMat, cv::Point(pcx, pcy), 1, cv::Scalar(255, 255, 255));
|
||
// }
|
||
// }
|
||
// }
|
||
// }
|
||
// LeaveCriticalSection(&m_Jc->m_cs);
|
||
// m_GraftShowImage->Init();
|
||
// }
|
||
// else{
|
||
// m_GraftShowImage->m_ImageMat.release();
|
||
// m_GraftMat.release();
|
||
// unsigned char* imagetemp;
|
||
// int width = 0, height = 0, datasize = 0;
|
||
// m_Camera->GetOriginSize(width, height, datasize);
|
||
// if (datasize > 0) {
|
||
// imagetemp = new unsigned char[datasize];
|
||
// if (m_Camera->GetRawImage(imagetemp))
|
||
// {
|
||
// cv::Mat image(height, width, CV_8U, imagetemp);
|
||
// int rotate = -1;
|
||
// switch (m_ExtCfg->m_ShowImageRotateAngle)
|
||
// {
|
||
// case ExtCfg::Angle_0: {
|
||
// }break;
|
||
// case ExtCfg::Angle_90: {
|
||
// rotate = cv::ROTATE_90_COUNTERCLOCKWISE;
|
||
// }break;
|
||
// case ExtCfg::Angle_180: {
|
||
// rotate = cv::ROTATE_180;
|
||
// }break;
|
||
// case ExtCfg::Angle_270: {
|
||
// rotate = cv::ROTATE_90_CLOCKWISE;
|
||
// }break;
|
||
// }
|
||
// if (rotate >= 0) {
|
||
// cv::rotate(image, image, rotate);
|
||
// }
|
||
//
|
||
// if (m_BasePartImage) {
|
||
// delete m_BasePartImage;
|
||
// m_BasePartImage = NULL;
|
||
// }
|
||
// CalibrationShowInfo* info = new CalibrationShowInfo();
|
||
// info->Init(image, imagetemp);
|
||
// m_BasePartImage = info;
|
||
//
|
||
//
|
||
// if (m_CameraCalibrationCfg->m_CoverImageJoin && m_CoverImage) {
|
||
// cv::Mat divImage;
|
||
// cv::Scalar partMean = cv::mean(m_BasePartImage->m_ImageMat);
|
||
// cv::divide(m_BasePartImage->m_ImageMat, m_CoverImage->m_ImageMat, divImage, partMean.val[0]);
|
||
// if (m_CameraCalibrationCfg->m_ShowAssist) {
|
||
// m_GraftMat = CalibrationWithAssist(divImage);
|
||
// }
|
||
// else {
|
||
// m_GraftMat = CalibrationWithCfg(divImage);
|
||
// }
|
||
// }
|
||
// else {
|
||
// if (m_CameraCalibrationCfg->m_ShowAssist) {
|
||
// m_GraftMat = CalibrationWithAssist(m_BasePartImage->m_ImageMat);
|
||
// }
|
||
// else {
|
||
// m_GraftMat = CalibrationWithCfg(m_BasePartImage->m_ImageMat);
|
||
// }
|
||
// }
|
||
//
|
||
//
|
||
// //m_GraftShowImage->m_ImageMat.release();
|
||
//
|
||
// m_GraftMat.copyTo(m_GraftShowImage->m_ImageMat);
|
||
// cv::cvtColor(m_GraftShowImage->m_ImageMat, m_GraftShowImage->m_ImageMat, CV_GRAY2RGB);
|
||
// m_GraftShowImage->m_IsColor = true;
|
||
// //cv::imwrite("g:/calibration4/graf.bmp", m_GraftMat);
|
||
// m_ManualGraftSelectPart = 0;
|
||
// if (m_Jc->GetJob()) {
|
||
// int xLength = (m_CameraCalibrationCfg->m_PlatformTopRightX - m_CameraCalibrationCfg->m_PlatformTopLeftX)*m_CameraCalibrationCfg->m_MagnifyScale;
|
||
// int yLength = (m_CameraCalibrationCfg->m_PlatformTopRightY - m_CameraCalibrationCfg->m_PlatformBottomRightY)*m_CameraCalibrationCfg->m_MagnifyScale;
|
||
// int centerx = xLength / 2;
|
||
// int centery = yLength / 2;
|
||
// FileProcessor *jfp = m_Jc->GetJob();
|
||
// MetaData::Layer* player = jfp->GetLayer(0);
|
||
//
|
||
// for (unsigned int dbIndex = 0; dbIndex < player->data_blocks.size(); ++dbIndex) {
|
||
// MetaData::DataBlock* db = player->data_blocks[dbIndex];
|
||
// if (jfp->GetMetaData()->m_FirstLayerBlockMap.find(db) == jfp->GetMetaData()->m_FirstLayerBlockMap.end())continue;
|
||
// BPBinary::BinDataBlock* pdb = jfp->GetMetaData()->m_FirstLayerBlockMap[db];
|
||
// if (!pdb)continue;
|
||
// if (pdb->type != BIN_CHAIN)continue;
|
||
//
|
||
// MetaData::Part* cpart = jfp->GetMetaData()->GetPart(db->references->part);
|
||
//
|
||
// for (unsigned int j = 0; j < pdb->point_indexs.size(); ++j) {
|
||
// BPBinary::ChainPoint* point = (BPBinary::ChainPoint*)pdb->point_indexs[j];
|
||
// std::vector<cv::Point2f> line;
|
||
// int cc = j % 10;
|
||
// for (unsigned int k = 0; k < point->points.size(); ++k) {
|
||
// double real_x = point->points[k]->x;
|
||
// double real_y = point->points[k]->y;
|
||
// double mov_x1 = cpart->partPosBean.m_XOffset + point->points[k]->x;
|
||
// double mov_y1 = cpart->partPosBean.m_YOffset + point->points[k]->y;
|
||
// real_x = (mov_x1 - cpart->partPosBean.m_PartCenterX)*cos(cpart->partPosBean.m_Radians) - (mov_y1 - cpart->partPosBean.m_PartCenterY)*sin(cpart->partPosBean.m_Radians) + cpart->partPosBean.m_PartCenterX;
|
||
// real_y = (mov_x1 - cpart->partPosBean.m_PartCenterX)*sin(cpart->partPosBean.m_Radians) + (mov_y1 - cpart->partPosBean.m_PartCenterY)*cos(cpart->partPosBean.m_Radians) + cpart->partPosBean.m_PartCenterY;
|
||
//
|
||
// float tempx = real_x * m_CameraCalibrationCfg->m_MagnifyScale + centerx;
|
||
// float tempy = centery - real_y * m_CameraCalibrationCfg->m_MagnifyScale;
|
||
// cv::Point2f p(tempx, tempy);
|
||
// line.push_back(p);
|
||
// }
|
||
// if (!line.empty()) {
|
||
// if (m_CameraCalibrationCfg->m_ShowAssist) {
|
||
// cv::RotatedRect re = cv::minAreaRect(line);
|
||
// cv::Point2f P[4];
|
||
// re.points(P);
|
||
// for (int j = 0; j <= 3; j++)
|
||
// {
|
||
// cv::line(m_GraftShowImage->m_ImageMat, P[j], P[(j + 1) % 4], m_ColorMap[cc], 1);
|
||
// cv::circle(m_GraftShowImage->m_ImageMat, re.center, 1, m_ColorMap[cc]);
|
||
// }
|
||
// }
|
||
// int pcx = cpart->partPosBean.m_PartCenterX*m_CameraCalibrationCfg->m_MagnifyScale + centerx + 0.5;
|
||
// int pcy = centery - cpart->partPosBean.m_PartCenterY * m_CameraCalibrationCfg->m_MagnifyScale + 0.5;
|
||
// for (size_t lindex = 0; lindex < line.size() - 1; lindex++) {
|
||
// if (m_ManualGraftSelectPart == cpart->id) {
|
||
// cv::line(m_GraftShowImage->m_ImageMat, line[lindex], line[lindex + 1], cv::Scalar(0, 255, 0), 1);
|
||
// }
|
||
// else {
|
||
// cv::line(m_GraftShowImage->m_ImageMat, line[lindex], line[lindex + 1], cv::Scalar(255, 0, 0), 1);
|
||
// }
|
||
//
|
||
// }
|
||
// //cv::circle(m_GraftShowImage->m_ImageMat, cv::Point(pcx, pcy), 1, cv::Scalar(255, 255, 255));
|
||
// }
|
||
// }
|
||
//
|
||
// }
|
||
//
|
||
// }
|
||
//
|
||
// m_GraftShowImage->Init();
|
||
// }
|
||
// else {
|
||
// delete[] imagetemp;
|
||
// }
|
||
// }
|
||
// }
|
||
// }
|
||
// }
|
||
// ImGui::SameLine(0, 20);
|
||
// ImGui::Checkbox(g_LngManager->UI_ShowAssistInfo->ShowText(), &m_CameraCalibrationCfg->m_ShowAssist);
|
||
// ImGui::SameLine(0, 20);
|
||
// ImGui::Checkbox(g_LngManager->UI_BaseImageJoin->ShowText(), &m_CameraCalibrationCfg->m_CoverImageJoin);
|
||
//
|
||
//
|
||
// if (ImGui::Button(g_LngManager->UI_AutoGraft->ShowText()))
|
||
// {
|
||
// if (!m_CoverImage) {
|
||
// g_Toast->AddToast(new ToastBean(g_LngManager->Toast_BaseImageCheck->ShowText(), 3000, Toast::COLOR_RED));
|
||
// }
|
||
// else {
|
||
// if (!m_BasePartImage) {
|
||
// g_Toast->AddToast(new ToastBean(g_LngManager->Toast_PlatformImageCheck->ShowText(), 3000, Toast::COLOR_RED));
|
||
// }
|
||
// else {
|
||
// if (!m_Jc->GetJob()) {
|
||
// g_Toast->AddToast(new ToastBean(g_LngManager->Toast_TaskCheck->ShowText(), 3000, Toast::COLOR_RED));
|
||
// }
|
||
// else {
|
||
// ImGui::OpenPopup(g_LngManager->UI_AutoGrafting->ShowText());
|
||
// StartGraft();
|
||
// }
|
||
// }
|
||
// }
|
||
// }
|
||
//
|
||
//
|
||
// if (ImGui::BeginPopupModal(g_LngManager->UI_AutoGrafting->ShowText(), 0, ImGuiWindowFlags_AlwaysAutoResize)) {
|
||
// ImGui::ProgressBar(GetGraftProgress(), ImVec2(AutoSize::s200.calcValue, 0));
|
||
// string info = GetGraftInfo();
|
||
// if (info != "") {
|
||
// ImGui::Text(info.c_str());
|
||
// }
|
||
// if (ImGui::Button(g_LngManager->UI_Terminate->ShowText(), ImVec2(ImGui::GetWindowWidth() / 2 - 12, 0)))
|
||
// {
|
||
// StopGraft();
|
||
// }
|
||
// ImGui::SameLine();
|
||
// if (ImGui::Button(g_LngManager->UI_Exit->ShowText(), ImVec2(ImGui::GetWindowWidth() / 2 - 12, 0)))
|
||
// {
|
||
// ImGui::CloseCurrentPopup();
|
||
// }
|
||
// ImGui::EndPopup();
|
||
// }
|
||
//
|
||
// ImGui::PushItemWidth(AutoSize::s100.calcValue);
|
||
// ImGui::SameLine(0, 20);
|
||
// ImGui::InputScalar(g_LngManager->UI_MagnifyScale->ShowText(), ImGuiDataType_U32, &m_CameraCalibrationCfg->m_MagnifyScale, 0, 0, 0, ImGuiInputTextFlags_EnterReturnsTrue);
|
||
// ImGui::SameLine(0, 20);
|
||
// ImGui::InputFloat(g_LngManager->UI_MatchRatio->ShowText(), &m_CameraCalibrationCfg->m_MatchRatio, 0, 0, "%.1f", ImGuiInputTextFlags_EnterReturnsTrue);
|
||
// ImGui::SameLine(0, 20);
|
||
//
|
||
// ImGui::Checkbox(g_LngManager->UI_UseBinImage->ShowText(), &m_CameraCalibrationCfg->m_ShowBinImage);
|
||
// ImGui::SameLine(0, 20);
|
||
// ImGui::InputInt(g_LngManager->UI_GrayRef->ShowText(), &m_CameraCalibrationCfg->m_GrayRef, 0, 0, ImGuiInputTextFlags_EnterReturnsTrue);
|
||
// ImGui::SameLine(0, 20);
|
||
// ImGui::InputInt(g_LngManager->UI_GrayOffset->ShowText(), &m_CameraCalibrationCfg->m_BinaryThresholdOffset, 0, 0, ImGuiInputTextFlags_EnterReturnsTrue);
|
||
// ImGui::SameLine(0, 20);
|
||
// ImGui::Checkbox(g_LngManager->UI_BlackFace->ShowText(), &m_CameraCalibrationCfg->m_BlackFace);
|
||
// ImGui::PopItemWidth();
|
||
//
|
||
//
|
||
// ImGui::BeginGroup();
|
||
// ImGui::BeginChild("countours", ImVec2(1020, 0), false, ImGuiWindowFlags_HorizontalScrollbar);
|
||
// EnterCriticalSection(&m_GraftCS);
|
||
// if (m_IsGraftUpdated) {
|
||
// m_GraftShowImage->Update();
|
||
// m_IsGraftUpdated = false;
|
||
// }
|
||
// LeaveCriticalSection(&m_GraftCS);
|
||
// ImVec2 pos = ImGui::GetCursorScreenPos();
|
||
// ImGui::Image((ImTextureID)m_GraftShowImage->GetTex(), ImVec2(m_GraftShowImage->m_Width, m_GraftShowImage->m_High));
|
||
// ImGuiIO& io = ImGui::GetIO();
|
||
// if (ImGui::IsItemHovered())
|
||
// {
|
||
// m_LastMouRefGraftPosX = io.MousePos.x - pos.x + 0.5f;
|
||
// m_LastMouRefGraftPosY = io.MousePos.y - pos.y + 0.5f;
|
||
// float my_tex_w = m_GraftShowImage->m_Width;
|
||
// float my_tex_h = m_GraftShowImage->m_High;
|
||
//
|
||
// ImGui::BeginTooltip();
|
||
// float region_sz = 60.0f;
|
||
// float region_x = io.MousePos.x - pos.x - region_sz * 0.5f; if (region_x < 0.0f) region_x = 0.0f; else if (region_x > my_tex_w - region_sz) region_x = my_tex_w - region_sz;
|
||
// float region_y = io.MousePos.y - pos.y - region_sz * 0.5f; if (region_y < 0.0f) region_y = 0.0f; else if (region_y > my_tex_h - region_sz) region_y = my_tex_h - region_sz;
|
||
//
|
||
// //float ratioX = my_tex_w / 1000.0f;
|
||
// //float ratioY = my_tex_h / 1000.0f;
|
||
// float zoom = 6.0f;
|
||
// //ImGui::Text("Min: (%.2f, %.2f)", region_x, region_y);
|
||
// //ImGui::Text("Max: (%.2f, %.2f)", region_x + region_sz, region_y + region_sz);
|
||
// if ((m_LastMouRefGraftPosX < my_tex_w) && (m_LastMouRefGraftPosY < my_tex_h) && m_GraftShowImage->m_ImageMat.data) {
|
||
// float r = m_GraftShowImage->m_ImageMat.at<cv::Vec3b>(m_LastMouRefGraftPosY, m_LastMouRefGraftPosX)[0];
|
||
// float g = m_GraftShowImage->m_ImageMat.at<cv::Vec3b>(m_LastMouRefGraftPosY, m_LastMouRefGraftPosX)[1];
|
||
// float b = m_GraftShowImage->m_ImageMat.at<cv::Vec3b>(m_LastMouRefGraftPosY, m_LastMouRefGraftPosX)[2];
|
||
// m_LastGrayRef = r*0.3f + g*0.6f + b*0.1f;
|
||
// }
|
||
// ImGui::Text("Pos: (%.3f, %.3f , %u)", m_LastMouRefGraftPosX, m_LastMouRefGraftPosY, m_LastGrayRef);
|
||
//
|
||
// ImVec2 uv0 = ImVec2((region_x) / my_tex_w, (region_y) / my_tex_h);
|
||
// ImVec2 uv1 = ImVec2((region_x + region_sz) / my_tex_w, (region_y + region_sz) / my_tex_h);
|
||
// //
|
||
// ImGui::GetWindowDrawList();
|
||
// //draw_list->AddImage((ImTextureID)m_LocationImage->GetTex(), ImVec2(0, 0), ImVec2(region_sz * zoom, region_sz * zoom), uv0, uv1, ImColor(255, 255, 255, 255));
|
||
// ImGui::ImageCross((ImTextureID)m_GraftShowImage->GetTex(), ImVec2(region_sz * zoom, region_sz * zoom), uv0, uv1, ImColor(255, 255, 255, 255), ImColor(255, 255, 255, 128), ImVec2((m_LastMouRefGraftPosX - region_x) * zoom, (m_LastMouRefGraftPosY - region_y) * zoom));
|
||
// ImGui::EndTooltip();
|
||
// }
|
||
// if (m_Jc->GetJob()) {
|
||
// MetaData::Part* sPart = m_Jc->GetJob()->GetMetaData()->GetPart(m_ManualGraftSelectPart);
|
||
// if (sPart) {
|
||
// if (ImGui::BeginPopupContextItem("SetPoint"))
|
||
// {
|
||
// if (ImGui::Button(g_LngManager->UI_DataPoint1->ShowText(), ImVec2(80, 0))) {
|
||
// sPart->partPosBean.m_SelectDataPoint1[0] = m_LastMouRefGraftPosX;
|
||
// sPart->partPosBean.m_SelectDataPoint1[1] = m_LastMouRefGraftPosY;
|
||
// ImGui::CloseCurrentPopup();
|
||
// }
|
||
// if (ImGui::Button(g_LngManager->UI_ImagePoint1->ShowText(), ImVec2(80, 0))) {
|
||
// sPart->partPosBean.m_SelectImagePoint1[0] = m_LastMouRefGraftPosX;
|
||
// sPart->partPosBean.m_SelectImagePoint1[1] = m_LastMouRefGraftPosY;
|
||
// ImGui::CloseCurrentPopup();
|
||
// }
|
||
// if (ImGui::Button(g_LngManager->UI_DataPoint2->ShowText(), ImVec2(80, 0))) {
|
||
// sPart->partPosBean.m_SelectDataPoint2[0] = m_LastMouRefGraftPosX;
|
||
// sPart->partPosBean.m_SelectDataPoint2[1] = m_LastMouRefGraftPosY;
|
||
// ImGui::CloseCurrentPopup();
|
||
// }
|
||
// if (ImGui::Button(g_LngManager->UI_ImagePoint2->ShowText(), ImVec2(80, 0))) {
|
||
// sPart->partPosBean.m_SelectImagePoint2[0] = m_LastMouRefGraftPosX;
|
||
// sPart->partPosBean.m_SelectImagePoint2[1] = m_LastMouRefGraftPosY;
|
||
// ImGui::CloseCurrentPopup();
|
||
// }
|
||
// if (ImGui::Button(g_LngManager->UI_GrayOffset->ShowText())) {
|
||
// m_CameraCalibrationCfg->m_GrayRef = m_LastGrayRef;
|
||
// ImGui::CloseCurrentPopup();
|
||
// }
|
||
// if (ImGui::Button(g_LngManager->UI_Exit->ShowText(), ImVec2(80, 0))) {
|
||
// ImGui::CloseCurrentPopup();
|
||
// }
|
||
// ImGui::EndPopup();
|
||
// }
|
||
// }
|
||
// }
|
||
//
|
||
// ImGui::EndChild();
|
||
// ImGui::EndGroup();
|
||
//
|
||
// ImGui::SameLine();
|
||
// ImGui::SeparatorEx(ImGuiSeparatorFlags_Vertical);
|
||
// ImGui::SameLine();
|
||
//
|
||
// ImGui::BeginGroup();
|
||
// ImGui::BeginChild("grafttask", ImVec2(AutoSize::s300.calcValue, 0), false);
|
||
// EnterCriticalSection(&m_Jc->m_cs);
|
||
// if (m_Jc->GetJob()) {
|
||
// int xLength = (m_CameraCalibrationCfg->m_PlatformTopRightX - m_CameraCalibrationCfg->m_PlatformTopLeftX)*m_CameraCalibrationCfg->m_MagnifyScale;
|
||
// int yLength = (m_CameraCalibrationCfg->m_PlatformTopRightY - m_CameraCalibrationCfg->m_PlatformBottomRightY)*m_CameraCalibrationCfg->m_MagnifyScale;
|
||
// int centerx = xLength / 2;
|
||
// int centery = yLength / 2;
|
||
// FileProcessor *jfp = m_Jc->GetJob();
|
||
// vector<MetaData::Part*> &partVec = jfp->GetMetaData()->GetPartVec();
|
||
// char buffer[1024];
|
||
// bool is_changed = false;
|
||
// for (size_t partIndex = 0; partIndex < partVec.size(); ++partIndex) {
|
||
// MetaData::Part* part = partVec[partIndex];
|
||
// sprintf_s(buffer, sizeof(buffer), u8"%d_%s", part->id, part->name.c_str());
|
||
// if (ImGui::TreeNode(buffer)) {
|
||
//
|
||
// ImGui::PushItemWidth(AutoSize::s120.calcValue);
|
||
// sprintf_s(buffer, sizeof(buffer), "%d_xoffset", part->id);
|
||
// ImGui::PushID(buffer);
|
||
// if (ImGui::InputFloat(g_LngManager->Main_OffsetX->ShowText(), &part->partPosBean.m_XOffset, 0, 0, "%.6f", ImGuiInputTextFlags_EnterReturnsTrue)) {
|
||
// is_changed = true;
|
||
// g_log->TraceInfo(g_LngManager->Log_SetPartOffsetX->ShowText(), part->id, part->partPosBean.m_XOffset);
|
||
// }
|
||
// ImGui::PopID();
|
||
// sprintf_s(buffer, sizeof(buffer), "%d_yoffset", part->id);
|
||
// ImGui::PushID(buffer);
|
||
// if (ImGui::InputFloat(g_LngManager->Main_OffsetY->ShowText(), &part->partPosBean.m_YOffset, 0, 0, "%.6f", ImGuiInputTextFlags_EnterReturnsTrue)) {
|
||
// is_changed = true;
|
||
// g_log->TraceInfo(g_LngManager->Log_SetPartOffsetY->ShowText(), part->id, part->partPosBean.m_YOffset);
|
||
// }
|
||
// ImGui::PopID();
|
||
// sprintf_s(buffer, sizeof(buffer), "%d_rotate", part->id);
|
||
// ImGui::PushID(buffer);
|
||
// if (ImGui::InputFloat(g_LngManager->Main_RotateAngle->ShowText(), &part->partPosBean.m_RotateAngle, 0, 0, "%.6f", ImGuiInputTextFlags_EnterReturnsTrue)) {
|
||
// is_changed = true;
|
||
// g_log->TraceInfo(g_LngManager->Log_SetPartRotate->ShowText(), part->id, part->partPosBean.m_RotateAngle);
|
||
// }
|
||
// ImGui::PopID();
|
||
// ImGui::PopItemWidth();
|
||
// if (ImGui::RadioButton(g_LngManager->UI_HalfAutoGraft->ShowText(), &m_ManualGraftSelectPart, part->id))
|
||
// {
|
||
// is_changed = true;
|
||
// }
|
||
// if (m_ManualGraftSelectPart == part->id) {
|
||
// ImGui::InputFloat2(g_LngManager->UI_DataPoint1->ShowText(), part->partPosBean.m_SelectDataPoint1, "%.6f", ImGuiInputTextFlags_EnterReturnsTrue);
|
||
// ImGui::InputFloat2(g_LngManager->UI_ImagePoint1->ShowText(), part->partPosBean.m_SelectImagePoint1, "%.6f", ImGuiInputTextFlags_EnterReturnsTrue);
|
||
// ImGui::InputFloat2(g_LngManager->UI_DataPoint2->ShowText(), part->partPosBean.m_SelectDataPoint2, "%.6f", ImGuiInputTextFlags_EnterReturnsTrue);
|
||
// ImGui::InputFloat2(g_LngManager->UI_ImagePoint2->ShowText(), part->partPosBean.m_SelectImagePoint2, "%.6f", ImGuiInputTextFlags_EnterReturnsTrue);
|
||
// if (ImGui::Button(g_LngManager->UI_Calc->ShowText())) {
|
||
// float x1 = part->partPosBean.m_SelectDataPoint1[0] - centerx;
|
||
// float y1 = (float)centery - part->partPosBean.m_SelectDataPoint1[1];
|
||
// float x2 = part->partPosBean.m_SelectDataPoint2[0] - centerx;
|
||
// float y2 = (float)centery - part->partPosBean.m_SelectDataPoint2[1];
|
||
// float x3 = part->partPosBean.m_SelectImagePoint1[0] - centerx;
|
||
// float y3 = (float)centery - part->partPosBean.m_SelectImagePoint1[1];
|
||
// float x4 = part->partPosBean.m_SelectImagePoint2[0] - centerx;
|
||
// float y4 = (float)centery - part->partPosBean.m_SelectImagePoint2[1];
|
||
//
|
||
// double epsilon = 1.0e-6;
|
||
// double dist1 = sqrt((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1));
|
||
// double xtemp1 = (x2 - x1) / dist1;
|
||
// double ytemp1 = (y2 - y1) / dist1;
|
||
// double dist2 = sqrt((x4 - x3)*(x4 - x3) + (y4 - y3)*(y4 - y3));
|
||
// double xtemp2 = (x4 - x3) / dist2;
|
||
// double ytemp2 = (y4 - y3) / dist2;
|
||
// double dot = xtemp1 * xtemp2 + ytemp1 * ytemp2;
|
||
// double angle = 0.0f;
|
||
// if (fabs(dot - 1.0) <= epsilon)
|
||
// angle = 0.0;
|
||
// else if (fabs(dot + 1.0) <= epsilon)
|
||
// angle = acos(-1.0);
|
||
// else {
|
||
// float cross = 0.0;
|
||
// angle = acos(dot);
|
||
// cross = xtemp1* ytemp2 - xtemp2 * ytemp1;
|
||
// if (cross < 0) {
|
||
// angle = 2.0 * acos(-1.0) - angle;
|
||
// }
|
||
// }
|
||
// double degree = angle * 180.0 / acos(-1.0);
|
||
//
|
||
// float centerx = part->partPosBean.m_PartCenterX*m_CameraCalibrationCfg->m_MagnifyScale;
|
||
// float centery = part->partPosBean.m_PartCenterY*m_CameraCalibrationCfg->m_MagnifyScale;
|
||
//
|
||
// float real_x = (x1 - centerx)*cos(MathHelper::DegreesToRadians(degree)) - (y1 - centery)*sin(MathHelper::DegreesToRadians(degree)) + centerx;
|
||
// float real_y = (x1 - centerx)*sin(MathHelper::DegreesToRadians(degree)) + (y1 - centery)*cos(MathHelper::DegreesToRadians(degree)) + centery;
|
||
//
|
||
// float fixx = (x3 - real_x) / m_CameraCalibrationCfg->m_MagnifyScale;
|
||
// float fixy = (y3 - real_y) / m_CameraCalibrationCfg->m_MagnifyScale;
|
||
// part->partPosBean.m_XOffset += fixx;
|
||
// part->partPosBean.m_YOffset += fixy;
|
||
// part->partPosBean.m_RotateAngle = degree;
|
||
// part->partPosBean.m_Radians = (float)MathHelper::DegreesToRadians(part->partPosBean.m_RotateAngle);
|
||
// part->partPosBean.m_PartCenterX = part->partPosBean.m_XOffset + part->partPosBean.m_SrcPartCenterX;
|
||
// part->partPosBean.m_PartCenterY = part->partPosBean.m_YOffset + part->partPosBean.m_SrcPartCenterY;
|
||
// is_changed = true;
|
||
// }
|
||
// }
|
||
// ImGui::TreePop();
|
||
//
|
||
// }
|
||
// }
|
||
//
|
||
//
|
||
// if (is_changed && m_GraftMat.data)
|
||
// {
|
||
// for (size_t partIndex = 0; partIndex < partVec.size(); ++partIndex) {
|
||
// MetaData::Part* part = partVec[partIndex];
|
||
// part->partPosBean.m_Radians = (float)MathHelper::DegreesToRadians(part->partPosBean.m_RotateAngle);
|
||
// part->partPosBean.m_PartCenterX = part->partPosBean.m_XOffset + part->partPosBean.m_SrcPartCenterX;
|
||
// part->partPosBean.m_PartCenterY = part->partPosBean.m_YOffset + part->partPosBean.m_SrcPartCenterY;
|
||
// Part* prev_part = m_PrevRenderer->GetPart(part->id);
|
||
// if (prev_part)prev_part->UpdateOffset();
|
||
// Part* print_part = m_Renderer->GetPart(part->id);
|
||
// if (print_part)print_part->UpdateOffset();
|
||
// }
|
||
//
|
||
// //m_CalibrationSrc.copyTo(m_CalibrationShow);
|
||
// m_GraftShowImage->m_ImageMat.release();
|
||
// m_GraftMat.copyTo(m_GraftShowImage->m_ImageMat);
|
||
// cv::cvtColor(m_GraftShowImage->m_ImageMat, m_GraftShowImage->m_ImageMat, cv::COLOR_GRAY2RGB);
|
||
// m_GraftShowImage->m_IsColor = true;
|
||
// MetaData::Layer* player = jfp->GetLayer(0);
|
||
//
|
||
//
|
||
// for (unsigned int dbIndex = 0; dbIndex < player->data_blocks.size(); ++dbIndex) {
|
||
// MetaData::DataBlock* db = player->data_blocks[dbIndex];
|
||
// if (jfp->GetMetaData()->m_FirstLayerBlockMap.find(db) == jfp->GetMetaData()->m_FirstLayerBlockMap.end())continue;
|
||
// BPBinary::BinDataBlock* pdb = jfp->GetMetaData()->m_FirstLayerBlockMap[db];
|
||
// if (!pdb)continue;
|
||
// if (pdb->type != BIN_CHAIN)continue;
|
||
//
|
||
// MetaData::Part* cpart = jfp->GetMetaData()->GetPart(db->references->part);
|
||
//
|
||
// for (unsigned int j = 0; j < pdb->point_indexs.size(); ++j) {
|
||
// BPBinary::ChainPoint* point = (BPBinary::ChainPoint*)pdb->point_indexs[j];
|
||
// std::vector<cv::Point2f> line;
|
||
// int cc = j % 10;
|
||
// for (unsigned int k = 0; k < point->points.size(); ++k) {
|
||
// double real_x = point->points[k]->x;
|
||
// double real_y = point->points[k]->y;
|
||
// double mov_x1 = cpart->partPosBean.m_XOffset + point->points[k]->x;
|
||
// double mov_y1 = cpart->partPosBean.m_YOffset + point->points[k]->y;
|
||
// real_x = (mov_x1 - cpart->partPosBean.m_PartCenterX)*cos(cpart->partPosBean.m_Radians) - (mov_y1 - cpart->partPosBean.m_PartCenterY)*sin(cpart->partPosBean.m_Radians) + cpart->partPosBean.m_PartCenterX;
|
||
// real_y = (mov_x1 - cpart->partPosBean.m_PartCenterX)*sin(cpart->partPosBean.m_Radians) + (mov_y1 - cpart->partPosBean.m_PartCenterY)*cos(cpart->partPosBean.m_Radians) + cpart->partPosBean.m_PartCenterY;
|
||
//
|
||
// float tempx = real_x * m_CameraCalibrationCfg->m_MagnifyScale + centerx;
|
||
// float tempy = centery - real_y * m_CameraCalibrationCfg->m_MagnifyScale;
|
||
// cv::Point2f p(tempx, tempy);
|
||
// line.push_back(p);
|
||
// }
|
||
// if (!line.empty()) {
|
||
// if (m_CameraCalibrationCfg->m_ShowAssist) {
|
||
// cv::RotatedRect re = cv::minAreaRect(line);
|
||
// cv::Point2f P[4];
|
||
// re.points(P);
|
||
// for (int j = 0; j <= 3; j++)
|
||
// {
|
||
// cv::line(m_GraftShowImage->m_ImageMat, P[j], P[(j + 1) % 4], m_ColorMap[cc], 1);
|
||
// cv::circle(m_GraftShowImage->m_ImageMat, re.center, 1, m_ColorMap[cc]);
|
||
// }
|
||
// }
|
||
// int pcx = cpart->partPosBean.m_PartCenterX*m_CameraCalibrationCfg->m_MagnifyScale + centerx + 0.5;
|
||
// int pcy = centery - cpart->partPosBean.m_PartCenterY * m_CameraCalibrationCfg->m_MagnifyScale + 0.5;
|
||
// for (size_t lindex = 0; lindex < line.size() - 1; lindex++) {
|
||
// if (m_ManualGraftSelectPart == cpart->id) {
|
||
// cv::line(m_GraftShowImage->m_ImageMat, line[lindex], line[lindex + 1], cv::Scalar(0, 255, 255), 1);
|
||
// }
|
||
// else {
|
||
// cv::line(m_GraftShowImage->m_ImageMat, line[lindex], line[lindex + 1], cv::Scalar(90, 0, 90), 1);
|
||
// }
|
||
//
|
||
// }
|
||
// //cv::circle(m_GraftShowImage->m_ImageMat, cv::Point(pcx, pcy), 1, cv::Scalar(255, 255, 255));
|
||
// }
|
||
//
|
||
// }
|
||
//
|
||
// }
|
||
// m_IsGraftUpdated = true;
|
||
// //UpdateCalibrationImage();
|
||
// }
|
||
// }
|
||
// LeaveCriticalSection(&m_Jc->m_cs);
|
||
//
|
||
// ImGui::EndChild();
|
||
// ImGui::EndGroup();
|
||
//
|
||
//
|
||
// ImGui::EndTabItem();
|
||
// }
|
||
// ImGui::EndTabBar();
|
||
//
|
||
// ImGui::End();
|
||
//}
|
||
|
||
void Calibration::TackPhotoAndSave()
|
||
{
|
||
|
||
unsigned char* imagetemp;
|
||
int width = 0, height = 0, datasize = 0;
|
||
m_Camera->GetOriginSize(width, height, datasize);
|
||
if (datasize > 0) {
|
||
imagetemp = new unsigned char[datasize];
|
||
if (m_Camera->GetRawImage(imagetemp))
|
||
{
|
||
cv::Mat image(height, width, CV_8U, imagetemp);
|
||
int rotate = -1;
|
||
switch (m_ExtCfg->m_ShowImageRotateAngle)
|
||
{
|
||
case ExtCfg::Angle_0: {
|
||
}break;
|
||
case ExtCfg::Angle_90: {
|
||
rotate = cv::ROTATE_90_COUNTERCLOCKWISE;
|
||
}break;
|
||
case ExtCfg::Angle_180: {
|
||
rotate = cv::ROTATE_180;
|
||
}break;
|
||
case ExtCfg::Angle_270: {
|
||
rotate = cv::ROTATE_90_CLOCKWISE;
|
||
}break;
|
||
}
|
||
if (rotate >= 0) {
|
||
cv::rotate(image, image, rotate);
|
||
}
|
||
time_t t;
|
||
time(&t);
|
||
char buffer[256];
|
||
sprintf_s(buffer, sizeof(buffer), "%s%lld.bmp", m_ImgPath.c_str(), t);
|
||
cv::imwrite(buffer, image);
|
||
image.release();
|
||
}
|
||
else {
|
||
delete[] imagetemp;
|
||
}
|
||
}
|
||
}
|
||
|
||
CalibrationShowInfo* Calibration::m_CoverImage = NULL; |