imwrite()函数
生活随笔
收集整理的這篇文章主要介紹了
imwrite()函数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
imwrite()函數的具體用法。
bool?imwrite(const string&?filename, InputArray?img, const vector<int>&?params=vector<int>()?)
該函數是把程序中的Mat類型的矩陣保存為圖像到指定位置。
opencv代碼:
#include <iostream> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp>using namespace std; using namespace cv;Mat src; Mat image; string str = "./";/*創建alpha表,整體偏紅色,左上角到右下角呈現從完全透明到完全不透明變化趨勢*/ void createAlphaMat(Mat &mat) {for (int i = 0; i < mat.rows; ++i) {for (int j = 0; j < mat.cols; ++j) {Vec4b& rgba = mat.at<Vec4b>(i, j);rgba[0] = UCHAR_MAX; //r分量一直最大,所以整體偏紅rgba[1] = saturate_cast<uchar>((float (mat.cols - j)) / ((float)mat.cols) * UCHAR_MAX);rgba[2] = saturate_cast<uchar>((float (mat.rows - i)) / ((float)mat.rows) * UCHAR_MAX);rgba[3] = saturate_cast<uchar>(0.5 * (rgba[1] + rgba[2]));}} }int main() {/*采用默認參數進行圖片的保存*/src = imread("test.jpg");imwrite(str+"原圖.jpg", src); //c版本中的保存圖片為cvSaveImage()函數,c++版本中直接與matlab的相似,imwrite()函數。imshow("src", src);Rect rect(src.cols/4, src.rows/4, src.cols/2, src.rows/2);image = src(rect);imwrite(str+"截取原圖中的一部分區域小圖.jpg", image);imshow("image", image);/*采用自己設置的參數來保存圖片*/Mat mat(480, 640, CV_8UC4);createAlphaMat(mat);vector<int> compression_params;compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);compression_params.push_back(9); //png格式下,默認的參數為3.try {imwrite("alpha.png", mat, compression_params);}catch (runtime_error& ex) {fprintf(stderr, "Exception converting image to PNG format: %s\n", ex.what());return 1;}fprintf(stdout, "Saved PNG file with alpha data.\n");waitKey(0);return 0; }總結
以上是生活随笔為你收集整理的imwrite()函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OpenCV中矩阵的归一化*(Norma
- 下一篇: 图像----滑动条