|
@@ -0,0 +1,129 @@
|
|
|
+#include <opencv.hpp>
|
|
|
+#include <math.h>
|
|
|
+#include <io.h>
|
|
|
+#include "chessboard.h"
|
|
|
+#include "utils.h"
|
|
|
+
|
|
|
+namespace graft_cv {
|
|
|
+ CChessboard::CChessboard(
|
|
|
+ ConfigParam& cp,
|
|
|
+ CGcvLogger*pLog)
|
|
|
+ :m_cp(cp),
|
|
|
+ m_pLogger(pLog),
|
|
|
+ m_dtype(img_type::tea_grab)
|
|
|
+ {
|
|
|
+ }
|
|
|
+ CChessboard::~CChessboard() {};
|
|
|
+ int CChessboard::detect(
|
|
|
+ ImgInfo*imginfo,
|
|
|
+ PositionInfo& posinfo,
|
|
|
+ int corner_row,
|
|
|
+ int corner_col,
|
|
|
+ int x0,
|
|
|
+ int y0,
|
|
|
+ int width,
|
|
|
+ int height,
|
|
|
+ const char* fn)
|
|
|
+ {
|
|
|
+ //1 load data
|
|
|
+ load_data(imginfo, fn);
|
|
|
+
|
|
|
+ //2
|
|
|
+ if (m_cp.image_show) {
|
|
|
+ cv::Rect rc = cv::Rect(x0, y0, width, height);
|
|
|
+ cv::Mat img_tmp = m_raw_img.clone();
|
|
|
+ cv::rectangle(img_tmp, rc, cv::Scalar(0, 0, 255),3);
|
|
|
+ imshow_wait("crop", img_tmp);
|
|
|
+ }
|
|
|
+ cv::Mat crop_img = m_raw_img(cv::Range(y0, y0 + height),cv::Range(x0, x0 + width));
|
|
|
+ if (m_cp.image_show) {
|
|
|
+ imshow_wait("cropped", crop_img);
|
|
|
+ }
|
|
|
+ //3
|
|
|
+ std::vector<cv::Point2f> pointbuf;
|
|
|
+ bool found = cv::findChessboardCorners(
|
|
|
+ crop_img,
|
|
|
+ cv::Size(corner_row, corner_col),
|
|
|
+ pointbuf);
|
|
|
+ if (found){
|
|
|
+ cv::Mat tmp_img = crop_img.clone();
|
|
|
+ if (m_pLogger) {
|
|
|
+ stringstream buff;
|
|
|
+ buff << m_imgId << m_dtype_str << "chessboard corners:\nx\ty\n";
|
|
|
+ for (auto&p : pointbuf) {
|
|
|
+ buff << p.x+x0 << ", " << p.y+y0 << "\n";
|
|
|
+ }
|
|
|
+ m_pLogger->INFO(buff.str());
|
|
|
+ }
|
|
|
+
|
|
|
+ cv::drawChessboardCorners(tmp_img, cv::Size(17, 18), cv::Mat(pointbuf), found);
|
|
|
+ if (m_ppImgSaver && *m_ppImgSaver) {
|
|
|
+ (*m_ppImgSaver)->saveImage(tmp_img, m_imgId + "_rst_0");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ if (m_pLogger) {
|
|
|
+ stringstream buff;
|
|
|
+ buff << m_imgId << m_dtype_str << "image, NOT found chessboard corners";
|
|
|
+ m_pLogger->INFO(buff.str());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ int CChessboard::load_data(
|
|
|
+ ImgInfo*imginfo,
|
|
|
+ const char* fn) {
|
|
|
+ //数据加载功能实现,并生成imageid,保存原始数据到文件
|
|
|
+ int rst = 0;
|
|
|
+ //generate image id
|
|
|
+ if (m_dtype == img_type::tea_grab) {
|
|
|
+ m_imgId = getImgId(img_type::tea_grab);
|
|
|
+ m_dtype_str = string(" tea_grab_chessboard ");
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ m_imgId = getImgId(img_type::tea_cut);
|
|
|
+ m_dtype_str = string(" tea_cut_chessboard ");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (imginfo) {
|
|
|
+ if (m_pLogger) {
|
|
|
+ stringstream buff;
|
|
|
+ buff << m_imgId << m_dtype_str << "image, width=" << imginfo->width
|
|
|
+ << "\theight=" << imginfo->height;
|
|
|
+ m_pLogger->INFO(buff.str());
|
|
|
+ }
|
|
|
+ if (!isvalid(imginfo)) {
|
|
|
+ if (m_pLogger) {
|
|
|
+ m_pLogger->ERRORINFO(m_imgId + m_dtype_str + "input image invalid.");
|
|
|
+ }
|
|
|
+ throw_msg(m_imgId + " invalid input image");
|
|
|
+
|
|
|
+ }
|
|
|
+ m_raw_img = imginfo2mat(imginfo);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ cv::Mat img = imread(fn, cv::IMREAD_COLOR);
|
|
|
+ if (img.empty()) {
|
|
|
+ if (m_pLogger) {
|
|
|
+ m_pLogger->ERRORINFO(m_imgId + m_dtype_str + "input image invalid:" + string(fn));
|
|
|
+ }
|
|
|
+ throw_msg(m_imgId + m_dtype_str + "invalid input image: " + string(fn));
|
|
|
+
|
|
|
+ }
|
|
|
+ if (m_pLogger) {
|
|
|
+ stringstream buff;
|
|
|
+ buff << m_imgId << m_dtype_str << "image, width=" << img.cols
|
|
|
+ << "\theight=" << img.rows;
|
|
|
+ m_pLogger->INFO(buff.str());
|
|
|
+ }
|
|
|
+
|
|
|
+ m_raw_img = img.clone();
|
|
|
+ }
|
|
|
+ //image saver
|
|
|
+ if (m_ppImgSaver && *m_ppImgSaver) {
|
|
|
+ (*m_ppImgSaver)->saveImage(m_raw_img, m_imgId);
|
|
|
+ }
|
|
|
+ return rst;
|
|
|
+ }
|
|
|
+}
|