You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Cigarette/CigaretteSingle/alg.cpp

392 lines
12 KiB
C++

#include <iostream>
#include "alg.h"
#include "AlgCommon.h"
/*
<EFBFBD><EFBFBD>ͼ<EFBFBD><EFBFBD>ʽ˵<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
1.<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD>ͼ<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һֱ<EFBFBD><EFBFBD><EFBFBD>£<EFBFBD>ֱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>ν<EFBFBD>ͼ<EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>ν<EFBFBD>ͼ<EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڽ<EFBFBD>ͼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҽ<EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ν<EFBFBD>ͼ״̬
2.<EFBFBD><EFBFBD>ͼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ե<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>εĽ<EFBFBD>ͼ<EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD>ʽһ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǽ<EFBFBD>ͼ<EFBFBD><EFBFBD><EFBFBD>ɺ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٴν<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ״̬<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>ֽ<EFBFBD>ͼ<EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD>ԭʼͼƬ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD>ԭʼͼƬ<EFBFBD><EFBFBD>δ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>ֽ<EFBFBD>ͼ<EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
#define MIN_ROI_AREA 300 //<2F><>Ч<EFBFBD><D0A7>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//<2F><><EFBFBD><EFBFBD>ȫ<EFBFBD>ֱ<EFBFBD><D6B1><EFBFBD>
static cv::Point g_pt; //<2F><EFBFBD><E6B4A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>ΰ<EFBFBD><CEB0>µ<EFBFBD><C2B5><EFBFBD><EFBFBD><EFBFBD><EAA3AC><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD><EFBFBD>Ͻ<EFBFBD><CFBD><EFBFBD><EFBFBD>
static cv::Mat g_img_src; //<2F><EFBFBD><E6B4A2>ǰ<EFBFBD><C7B0>ԭʼͼ<CABC><CDBC><EFBFBD><EFBFBD>
static cv::Mat g_img_dst; //<2F><EFBFBD><E6B4A2>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD>ʵʱ<CAB5><CAB1>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD>
static cv::Mat g_img_roi; //<2F><EFBFBD><E6B4A2>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD>ʱ<EFBFBD>ľֲ<C4BE>ͼ<EFBFBD><CDBC>
static cv::Rect roi_area; //<2F><EFBFBD><E6B4A2>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD>ʱ<EFBFBD>ľֲ<C4BE>ͼ<EFBFBD><CDBC>
static bool g_flag = false; //<2F>Ƿ<EFBFBD><C7B7>Ѵ<EFBFBD><D1B4>ڽ<EFBFBD>ͼ״̬<D7B4>ı<EFBFBD>ʾ
static bool g_issave = false; //<2F>Ƿ<EFBFBD><C7B7>Ѿ<EFBFBD><D1BE><EFBFBD>δ<EFBFBD><EFBFBD>Ļ<EFBFBD><C4BB><EFBFBD>roi<6F>ı<EFBFBD>ʾ
static std::string g_window_name = "image";//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1>roiͼ<69><CDBC>
void save_roi(cv::Mat image)
{
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD>imwrite()<29><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԣ<EFBFBD>
}
// <09><><EFBFBD><EFBFBD><EFBFBD>Ļص<C4BB><D8B5><EFBFBD><EFBFBD><EFBFBD>
static void onMouse(int event, int x, int y, int, void*)
{
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD>
if (cv::EVENT_LBUTTONDOWN == event)
{
//<2F><><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD>ǽ<EFBFBD>ͼ״̬<D7B4><CCAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ״̬<D7B4><CCAC><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0>Ϊ<EFBFBD><CEAA>ʼ<EFBFBD><EFBFBD><EFBFBD><E6B4A2>
if (g_flag == false)
{
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ״̬<D7B4><CCAC>
g_flag = true;
//<2F><EFBFBD><E6B4A2>ͼ<EFBFBD><CDBC>ʼ<EFBFBD><CABC>
g_pt = cv::Point(x, y);
//<2F><>δ<EFBFBD><CEB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>roi<6F>ı<EFBFBD><C4B1><EFBFBD><E6A3AC><EFBFBD><EFBFBD><EFBFBD>д洢<D0B4><E6B4A2>
if (g_issave == true)
{
save_roi(g_img_roi);
g_issave = false;
}
}
//<2F><><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD>ڽ<EFBFBD>ͼ״̬<D7B4><CCAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ״̬<D7B4><CCAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡroi
if (g_flag == true && abs(x - g_pt.x)*abs(y - g_pt.y) > MIN_ROI_AREA)
{
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ״̬
g_flag = false;
// <20><><EFBFBD>ý<EFBFBD>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>g_img_roi
roi_area = cv::Rect(g_pt, cv::Point(x, y));
g_img_src(roi_area).copyTo(g_img_roi);
//cv::imshow("roi", g_img_roi);
g_issave = true;
}
}//if(CV_EVENT_LBUTTONDOWN == event)
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD><C6B6>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڽ<EFBFBD>ͼ״̬ʱ
else if (cv::EVENT_MOUSEMOVE == event && g_flag)
{
//<2F><><EFBFBD>³<EFBFBD>ʼ<EFBFBD><CABC>g_img_dst<73><74>ȷ<EFBFBD><C8B7>g_img_dst<73><74>ʼ<EFBFBD><CABC>ֻ<EFBFBD><D6BB>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>
g_img_src.copyTo(g_img_dst);
cv::rectangle(g_img_dst, g_pt, cv::Point(x, y), cv::Scalar(255, 0, 0), 3, 8);
cv::imshow(g_window_name, g_img_dst);
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɿ<EFBFBD><C9BF>¼<EFBFBD>
else if (cv::EVENT_LBUTTONUP == event &&
abs(x - g_pt.x)*abs(y - g_pt.y) > MIN_ROI_AREA)
{
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ״̬
g_flag = false;
//<2F><>ȡroi
roi_area = cv::Rect(g_pt, cv::Point(x, y));
g_img_src(roi_area).copyTo(g_img_roi);
//cv::imshow("roi", g_img_roi);
g_issave = true;
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҽ<EFBFBD><D2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD>
else if (cv::EVENT_RBUTTONDOWN == event)
{
//<2F>ڽ<EFBFBD>ͼ״̬<D7B4><CCAC><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>ͼ״̬
if (g_flag == true)
{
g_flag = false;
//<2F><><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC>
cv::imshow(g_window_name, g_img_src);
}
//<2F><><EFBFBD>ڽ<EFBFBD>ͼ״̬<D7B4><CCAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч<EFBFBD><D0A7>һ<EFBFBD>ֵĽ<D6B5>ͼ<EFBFBD><CDBC><EFBFBD>
if (g_flag == false)
{
g_issave = false;
//<2F><><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC>
cv::imshow(g_window_name, g_img_src);
//<2F>رվֲ<D5BE><D6B2><EFBFBD><EFBFBD><EFBFBD>
//cv::destroyWindow("roi");
}
}
}
bool inline getMinMax(std::vector<cv::Point3i> &points, cv::Point3i &min, cv::Point3i &max)
{
int minx = 0, miny = 0, minz = 0;
int maxx = 0, maxy = 0, maxz = 0;
for (int i = 0; i < points.size(); i++) {
if (points[i].x < minx)minx = points[i].x;
if (points[i].y < miny)miny = points[i].y;
if (points[i].z < minz)minz = points[i].z;
if (points[i].x > maxx)maxx = points[i].x;
if (points[i].y > maxy)maxy = points[i].y;
if (points[i].z > maxz)maxz = points[i].z;
}
min.x = minx;
min.y = miny;
min.z = minz;
max.x = maxx;
max.y = maxy;
max.z = maxz;
return true;
}
bool Alg::ScreenShot(cv::Mat imgfull)
{
cv::namedWindow(g_window_name, cv::WINDOW_AUTOSIZE);
cv::setMouseCallback(g_window_name, onMouse, 0);
cv::Mat img = imgfull;
if (img.empty())
{
std::cout << "empty image" << std::endl;
return false;
}
//<2F>洢ԭʼͼ<CABC><CDBC>
g_img_src = img.clone();
cv::imshow(g_window_name, g_img_src);
while (1) {
cv::waitKey(100);
if(g_issave)break;
}
//ͼ<><CDBC>ѡ<EFBFBD>񴰿<EFBFBD>
cv::destroyWindow(g_window_name);
img = g_img_roi.clone();
if (img.empty())
{
std::cout << "empty roi image" << std::endl;
return false;
}
RefRect = roi_area;
return true;
}
//*****************ͼ<><CDBC><EFBFBD><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>*******************
void dftshift(cv::Mat &img_r, cv::Mat &img_i)
{
int cx = img_r.cols / 2;
int cy = img_r.rows / 2;//<2F><><EFBFBD>µIJ<C2B5><C4B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD>ͼ<EFBFBD><CDBC> (<28><>Ƶ<EFBFBD>Ƶ<EFBFBD><C6B5><EFBFBD><EFBFBD><EFBFBD>)
cv::Mat part1_r(img_r, cv::Rect(0, 0, cx, cy)); //Ԫ<><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾΪ(cx,cy)
cv::Mat part2_r(img_r, cv::Rect(cx, 0, cx, cy));
cv::Mat part3_r(img_r, cv::Rect(0, cy, cx, cy));
cv::Mat part4_r(img_r, cv::Rect(cx, cy, cx, cy));
cv::Mat temp;
part1_r.copyTo(temp); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>½<EFBFBD><C2BD><EFBFBD>λ<EFBFBD><CEBB><><CAB5>)
part4_r.copyTo(part1_r);
temp.copyTo(part4_r);
part2_r.copyTo(temp); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>½<EFBFBD><C2BD><EFBFBD>λ<EFBFBD><CEBB><><CAB5>)
part3_r.copyTo(part2_r);
temp.copyTo(part3_r);
cv::Mat part1_i(img_i, cv::Rect(0, 0, cx, cy)); //Ԫ<><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(cx,cy)
cv::Mat part2_i(img_i, cv::Rect(cx, 0, cx, cy));
cv::Mat part3_i(img_i, cv::Rect(0, cy, cx, cy));
cv::Mat part4_i(img_i, cv::Rect(cx, cy, cx, cy));
part1_i.copyTo(temp); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>½<EFBFBD><C2BD><EFBFBD>λ<EFBFBD><CEBB>(<28>鲿)
part4_i.copyTo(part1_i);
temp.copyTo(part4_i);
part2_i.copyTo(temp); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>½<EFBFBD><C2BD><EFBFBD>λ<EFBFBD><CEBB>(<28>鲿)
part3_i.copyTo(part2_i);
temp.copyTo(part3_i);
}
//*****************Ƶ<><C6B5><EFBFBD><EFBFBD><EFBFBD>˲<EFBFBD>*******************
cv::Mat freqfilt(cv::Mat &scr, cv::Mat &blur)
{
cv::Mat img_r = scr.clone();
cv::Mat img_i = cv::Mat::zeros(scr.size(), CV_32FC1);
//***********************DFT*******************
cv::Mat plane[] = { img_r, img_i}; //<2F><><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD><EFBFBD>洢dft<66><74><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD><EFBFBD>鲿<EFBFBD><E9B2BF>CV_32F<32><46><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
cv::Mat complexIm;
merge(plane, 2, complexIm);//<2F>ϲ<EFBFBD>ͨ<EFBFBD><CDA8> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϲ<EFBFBD>Ϊһ<CEAA><D2BB><32><CDA8><EFBFBD><EFBFBD>Mat<61><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
dft(complexIm, complexIm);//<2F><><EFBFBD>и<EFBFBD><D0B8><EFBFBD>Ҷ<EFBFBD><EFBFBD><E4BBBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//***************<2A><><EFBFBD>Ļ<EFBFBD>********************
split(complexIm, plane);//<2F><><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//plane[0] = plane[0](cv::Rect(0, 0, plane[0].cols & -2, plane[0].rows & -2));//<2F><><EFBFBD><EFBFBD>Ϊʲô&<26><>-2<><32><EFBFBD><EFBFBD><EFBFBD>鿴opencv<63>ĵ<EFBFBD>
// //<2F><>ʵ<EFBFBD><CAB5>Ϊ<EFBFBD>˰<EFBFBD><CBB0>к<EFBFBD><D0BA>б<EFBFBD><D0B1><EFBFBD>ż<EFBFBD><C5BC> -2<>Ķ<EFBFBD><C4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>11111111.......10 <20><><EFBFBD><EFBFBD>һλ<D2BB><CEBB>0
dftshift(plane[0], plane[1]);
//*****************<2A>˲<EFBFBD><CBB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>DFT<46><54><EFBFBD><EFBFBD><EFBFBD>ij˻<C4B3>****************
cv::Mat BLUR;
{
cv::Mat blur_r, blur_i;
multiply(plane[0], blur, blur_r); //<2F>˲<EFBFBD><CBB2><EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD><EFBFBD>˲<EFBFBD><CBB2><EFBFBD>ģ<EFBFBD><C4A3><EFBFBD><EFBFBD>ӦԪ<D3A6><D4AA><EFBFBD><EFBFBD><EFBFBD>ˣ<EFBFBD>
multiply(plane[1], blur, blur_i);//<2F>˲<EFBFBD><CBB2><EFBFBD><EFBFBD>鲿<EFBFBD><E9B2BF><EFBFBD>˲<EFBFBD><CBB2><EFBFBD>ģ<EFBFBD><C4A3><EFBFBD><EFBFBD>ӦԪ<D3A6><D4AA><EFBFBD><EFBFBD><EFBFBD>ˣ<EFBFBD>
cv::Mat plane1[] = { blur_r, blur_i };
merge(plane1, 2, BLUR);//ʵ<><CAB5><EFBFBD><EFBFBD><EFBFBD>鲿<EFBFBD>ϲ<EFBFBD>
}
//*********************<2A>õ<EFBFBD>ԭͼƵ<CDBC><C6B5>ͼ***********************************
//magnitude(plane[0], plane[1], plane[0]);//<2F><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD><30><CDA8>Ϊʵ<CEAA><CAB5>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD><31>鲿<EFBFBD><E9B2BF><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>ά<EFBFBD><CEAC><EFBFBD><EFBFBD>Ҷ<EFBFBD><EFBFBD><E4BBBB><EFBFBD><EFBFBD><EFBFBD>Ǹ<EFBFBD><C7B8><EFBFBD>
//plane[0] += cv::Scalar::all(1); //<2F><><EFBFBD><EFBFBD>Ҷ<EFBFBD><EFBFBD><E4BBBB><EFBFBD><EFBFBD>ͼƬ<CDBC><C6AC><EFBFBD>÷<EFBFBD><C3B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȽϺÿ<CFBA>
//log(plane[0], plane[0]); // float<61>͵ĻҶȿռ<C8BF>Ϊ[0<><30>1])
//normalize(plane[0], plane[0], 1, 0, cv::NORM_MINMAX); //<2F><>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ
//cv::Mat iiimg=plane[0];
//imshow("ԭͼ<D4AD><CDBC>Ƶ<EFBFBD><C6B5>ͼ",plane[0]);
idft(BLUR, BLUR); //idft<66><74><EFBFBD><EFBFBD>ҲΪ<D2B2><CEAA><EFBFBD><EFBFBD>
split(BLUR, plane);//<2F><><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA>ȡͨ<C8A1><CDA8>
magnitude(plane[0], plane[1], plane[0]); //<2F><><EFBFBD><EFBFBD>ֵ(ģ)
normalize(plane[0], plane[0], 1, 0, cv::NORM_MINMAX); //<2F><>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ
return plane[0];//<2F><><EFBFBD>ز<EFBFBD><D8B2><EFBFBD>
}
cv::Mat ideal_lbrf_kernel(cv::Mat &scr, float sigma)
{
cv::Mat ideal_low_pass(scr.size(), CV_32FC1); //<2F><>CV_32FC1
cv::Mat gauss_low_pass(scr.size(), CV_32FC1); //<2F><>CV_32FC1
cv::Mat gauss_high_pass(scr.size(), CV_32FC1); //<2F><>CV_32FC1
////*****************<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD>˲<EFBFBD><CBB2><EFBFBD>***********************
//float d0 = sigma;//<2F>뾶D0ԽС<D4BD><D0A1>ģ<EFBFBD><C4A3>Խ<EFBFBD>󣻰뾶D0Խ<30><D4BD><EFBFBD><EFBFBD>ģ<EFBFBD><C4A3>ԽС
//for (int i = 0; i < scr.rows; i++) {
// for (int j = 0; j < scr.cols; j++) {
// double d = sqrt(pow((i - scr.rows / 2), 2) + pow((j - scr.cols / 2), 2));//<2F><><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD>Ƶ<EFBFBD><C6B5><EFBFBD><EFBFBD><EFBFBD>ĵľ<C4B5><C4BE><EFBFBD>,<2C><><EFBFBD><EFBFBD>pow<6F><77><EFBFBD><EFBFBD>Ϊfloat<61><74>
// if (d <= d0) {
// ideal_low_pass.at<float>(i, j) = 1;
// }
// else {
// ideal_low_pass.at<float>(i, j) = 0;
// }
// }
//}
//std::string name = "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD>˲<EFBFBD><CBB2><EFBFBD>d0=" + std::to_string(sigma);
//*****************<2A><>˹<EFBFBD><CBB9>ͨ<EFBFBD><CDA8>ͨ<EFBFBD>˲<EFBFBD><CBB2><EFBFBD>***********************
//float sigma_gauss_low = sigma;
//for (int i = 0; i < scr.rows; i++) {
// for (int j = 0; j < scr.cols; j++) {
// double d = sqrt(pow((i - scr.rows / 2), 2) + pow((j - scr.cols / 2), 2));//<2F><><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD>pow<6F><77><EFBFBD><EFBFBD>Ϊfloat<61><74>
// double gauss_value = -1 * pow(d, 2) / (2 * pow(sigma_gauss_low, 2));
// gauss_value = pow(2.718281828459, gauss_value);
// gauss_low_pass.at<float>(i, j) = gauss_value;
// }
//}
//name = "<22><>˹<EFBFBD><CBB9>ͨ<EFBFBD>˲<EFBFBD><CBB2><EFBFBD>d0=" + std::to_string(sigma);
//*****************<2A><>˹<EFBFBD><CBB9>ͨ<EFBFBD><CDA8>ͨ<EFBFBD>˲<EFBFBD><CBB2><EFBFBD>***********************
float sigma_gauss_high = sigma;
for (int i = 0; i < scr.rows; i++) {
for (int j = 0; j < scr.cols; j++) {
double d = sqrt(pow((i - scr.rows / 2), 2) + pow((j - scr.cols / 2), 2));//<2F><><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD>pow<6F><77><EFBFBD><EFBFBD>Ϊfloat<61><74>
double gauss_value = -1 * pow(d, 2) / (2 * pow(sigma_gauss_high, 2));
gauss_value = pow(2.718281828459, gauss_value);
gauss_high_pass.at<float>(i, j) = 1-gauss_value;
}
}
//name = "<22><>˹<EFBFBD><CBB9>ͨ<EFBFBD>˲<EFBFBD><CBB2><EFBFBD>d0=" + std::to_string(sigma);
return gauss_high_pass;
}
cv::Mat ideal_Low_Pass_Filter(cv::Mat &src, float sigma)
{
int M = cv::getOptimalDFTSize(src.rows);
int N = cv::getOptimalDFTSize(src.cols);
cv::Mat padded; //<2F><><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD><EFBFBD>ٸ<EFBFBD><D9B8><EFBFBD>Ҷ<EFBFBD>
copyMakeBorder(src, padded, 0, M - src.rows, 0, N - src.cols, cv::BORDER_CONSTANT, cv::Scalar::all(0));
padded.convertTo(padded, CV_32FC1); //<2F><>ͼ<EFBFBD><CDBC>ת<EFBFBD><D7AA>Ϊflaot<6F><74>
cv::Mat ideal_kernel = ideal_lbrf_kernel(padded, sigma);//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD>˲<EFBFBD><CBB2><EFBFBD>
cv::Mat result = freqfilt(padded, ideal_kernel);
return result;
}
void Alg::process_2dfft(cv::Mat &input,cv::Mat &output)
{
double t = (double)cv::getTickCount();
cv::Mat img_clone = input.clone();
resize(img_clone, input,cv::Size(512,512));
cv::Mat img_thr;
threshold(img_clone, img_thr, 200, 255, cv::THRESH_TOZERO_INV);
threshold(img_thr, img_thr, 50, 255, cv::THRESH_BINARY);
cv::Mat pro_src = img_thr.clone();
fistEROthenDIL(pro_src,3, 3);
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
double maxarea = 0;
int maxAreaIdx = 0;
std::vector<std::vector<cv::Point> > contours_pre;
std::vector < cv::Vec4i > hierarchy_pre;
cv::findContours(pro_src, contours_pre, hierarchy_pre, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);
for (int i = 0; i < contours_pre.size(); i++)
{
double tmparea = fabs(contourArea(contours_pre[i]));
if (tmparea > maxarea)
{
maxarea = tmparea;
maxAreaIdx = i;//<2F><>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
}
cv::Mat second_input;
if (contours_pre.size())
{
cv::Rect rect_pre = cv::boundingRect(contours_pre[maxAreaIdx]);
second_input = img_clone(rect_pre).clone();
}
else
{
second_input = img_clone;
}
cv::Mat ideal = ideal_Low_Pass_Filter(second_input, 30);
ideal = ideal(cv::Rect(0, 0, second_input.cols, second_input.rows));
cv::Mat third_input;
normalize(ideal, third_input, 0, 255, cv::NORM_MINMAX);
third_input.convertTo(third_input, CV_8UC1);
threshold(third_input, third_input, 10, 255, cv::THRESH_BINARY);
fistEROthenDIL(third_input, 3, 5);
output = third_input.clone();
cv::waitKey(1);
t = ((double)cv::getTickCount() - t) / cv::getTickFrequency();
std::cout << "cost time\t" << t <<" s" << std::endl;
}
bool Alg::process_bool(cv::Mat &input, cv::Mat &output)
{
double t = (double)cv::getTickCount();
cv::Mat img_clone = input.clone();
cv::Mat img_input;
threshold(img_clone, img_clone, 200, 0, cv::THRESH_TOZERO_INV);
threshold(img_clone, img_input, 90, 0, cv::THRESH_TOZERO);
// third_input.convertTo(third_input, CV_8UC1);
fistEROthenDIL(img_input, 13, 13);
// output = third_input.clone();
// //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// cv::Mat temp = third_input.clone();
// double maxarea = 0;
// int maxAreaIdx = 0;
// std::vector<std::vector<cv::Point> > contours_pre;
// std::vector < cv::Vec4i > hierarchy_pre;
// cv::findContours(temp, contours_pre, hierarchy_pre, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);
// for (int i = 0; i < contours_pre.size(); i++)
// {
// drawContours(temp, contours_pre, i, cv::Scalar(255), 2, 8, hierarchy_pre);
// double tmparea = fabs(contourArea(contours_pre[i]));
// if (tmparea > maxarea)
// {
// maxarea = tmparea;
// maxAreaIdx = i;//<2F><>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// }
// }
t = ((double)cv::getTickCount() - t) / cv::getTickFrequency();
std::cout << "cost time\t" << t << " s" << std::endl;
return 1;
}