基於FPGA的Alpha半透明影象疊加演算法實現
阿新 • • 發佈:2019-01-06
#include<opencv2\opencv.hpp> #include<iostream> using namespace cv; using namespace std; Mat dealImg(Mat &img); int main() { Mat img = imread("E://ͼƬ//1.png"); if (img.empty()) { return -1; } Mat result; result = dealImg(img); waitKey(0); destroyAllWindows; return 0; } Mat dealImg(Mat & img){ Mat temp; Mat img1 = imread("E://ͼƬ//5.jpg"); if (img1.empty()) { return Mat(); } img.convertTo(temp, CV_32F); Mat img2(temp.size(), img1.type()); resize(img1, img2, temp.size()); img2.convertTo(img2, CV_32F); Mat result = temp.clone(); float alpha = 0.35; //調整透光 for (int i = 0; i < temp.rows; i++) { float *ptr_result = result.ptr<float>(i); float *ptr_temp = temp.ptr<float>(i); float *ptr_img1 = img2.ptr<float>(i); for (int j = 0; j < temp.cols; j++) { ptr_result[j * 3 + 0] = (1 - alpha)*ptr_img1[j * 3 + 0] + alpha*ptr_temp[j * 3 + 0]; ptr_result[j * 3 + 1] = (1 - alpha)*ptr_img1[j * 3 + 1] + alpha*ptr_temp[j * 3 + 1]; ptr_result[j * 3 + 2] = (1 - alpha)*ptr_img1[j * 3 + 2] + alpha*ptr_temp[j * 3 + 2]; } } result.convertTo(result, CV_8U); imshow("result", result); return result; }