当使用python验证了原理,同时获取了校正的角点参数,则可以直接使用C++代码对图像处理,
int process_image(unsigned char *imgBuffer, int width, int height, int channels, char* file_name)
{
int type = 0;
if (channels == 3) {
type = CV_8UC3;
}
else if (channels == 1) {
type = CV_8UC1;
}
Mat image_src = Mat(height, width, type);
memcpy(image_src.data, imgBuffer, width* height*channels);
//vector<Point2f> src_corner;
//src_corner.push_back(Point2f(0, 190));
//Point2f src_corner[4] = { Point2f(0, 190), Point2f(800, 200), Point2f(590, 460), Point2f(205, 460) };
if (src_corner[2].y < 100)
{
if (read_config() != 0)
{
return -2;
}
}
// TL BL BR BL
Point2f dst_corner[4] = { Point2f(0, 0), Point2f(static_cast<float>(m_camera_width), 0), Point2f(static_cast<float>(m_camera_width), static_cast<float>(m_camera_height)), Point2f(0, static_cast<float>(m_camera_height)) };
Mat result_image;
Mat warp_mat = getPerspectiveTransform(src_corner, dst_corner);
warpPerspective(image_src, result_image, warp_mat, Size(m_camera_width, m_camera_height));
//string save_file_name = get_date_time_string()+".bmp";
//strcpy_s(file_name, sizeof(save_file_name), save_file_name.c_str());
string save_file_name = file_name;
//Mat normalize_image;
//normalize(result_image, normalize_image, 255, 0, NORM_MINMAX);
//Mat equal_image;
//Mat blur_image;
//Mat Median_image;
//Mat bilate_image;
//vector<Mat> splitRGB(result_image.channels());
//cv::split(normalize_image, splitRGB);
//for (auto i = 0; i < result_image.channels(); i++) {
// equalizeHist(splitRGB[i], splitRGB[i]);
//}
//Mat mergeImage;
//merge(splitRGB, mergeImage);
//mergeImage.copyTo(equal_image);
//equal_image.convertTo(blur_image, -1, 1.5, 150);
//Mat dstImage;
//normalize_image.convertTo(dstImage, -1, 1.15,50);
//draw_for_test(result_image);
imwrite(save_file_name, result_image);
return 0;
}