雙線性插值Code
阿新 • • 發佈:2018-12-15
http://blog.csdn.net/carson2005/article/details/50460088
- void ImgResize_BiLinear()
- {
- cv::Mat imgSrc, imgDst1, imgDst2;
- imgSrc = cv::imread("test.jpg"
- imgDst1 = cv::Mat(cv::Size(imgSrc.cols*2, imgSrc.rows*2), imgSrc.type(), cv::Scalar::all(0));
- imgDst2 = cv::Mat(imgDst1.size(), imgSrc.type(), cv::Scalar::all(0));
- double scale_x = (double)imgSrc.cols / imgDst1.cols;
- double scale_y = (double)imgSrc.rows / imgDst1.rows;
- uchar* dataDst = imgDst1.data;
- int stepDst = imgDst1.step;
- uchar* dataSrc = imgSrc.data;
- int stepSrc = imgSrc.step;
- int iWidthSrc = imgSrc.cols;
- int iHiehgtSrc = imgSrc.rows;
- short cbufy[2];
- float y_float, x_float;
- int y_int, x_int;
- short cbufx[2];
- for (int j=0; j<imgDst1.rows; ++j)
- {
- y_float = (float)((j + 0.5) * scale_y - 0.5);
- y_int = cvFloor(y_float);
- y_float -= y_int;
- y_int = std::min(y_int, iHiehgtSrc - 2);
- y_int = std::max(0, y_int);
- cbufy[0] = cv::saturate_cast<short>((1.f - y_float) * 2048);
- cbufy[1] = 2048 - cbufy[0];
- for (int i=0; i<imgDst1.cols; ++i)
- {
- x_float = (float)((i + 0.5) * scale_x - 0.5);
- x_int = cvFloor(x_float);
- x_float -= x_int;
- if (x_int < 0)
- {
- x_float = 0, x_int = 0;
- }
- if (x_int >= iWidthSrc - 1)
- {
- x_float = 0, x_int = iWidthSrc - 2;
- }
- cbufx[0] = cv::saturate_cast<short>((1.f - x_float) * 2048);
- cbufx[1] = 2048 - cbufx[0];
- for (int k = 0; k < imgSrc.channels(); ++k)
- {
- *(dataDst+ j*stepDst + 3*i + k) = (*(dataSrc + y_int*stepSrc + 3*x_int + k) * cbufx[0] * cbufy[0] +
- *(dataSrc + (y_int+1)*stepSrc + 3*x_int + k) * cbufx[0] * cbufy[1] +
- *(dataSrc + y_int*stepSrc + 3*(x_int+1) + k) * cbufx[1] * cbufy[0] +
- *(dataSrc + (y_int+1)*stepSrc + 3*(x_int+1) + k) * cbufx[1] * cbufy[1]) >> 22;
- }
- }
- }
- cv::imwrite("result_1.jpg", imgDst1);
- cv::resize(imgSrc, imgDst2, imgDst1.size(), 0, 0, 1);
- cv::imwrite("result_2.jpg", imgDst2);
- }
上述程式碼為3通道彩圖的縮放程式碼,圖片的載入儲存用了Opencv的結構體,其餘基本保持C的風格;
VS2008+Opencv249下面測試通過,有興趣的朋友自己試試;- void ImgResize_BiLinear()
- {
- cv::Mat imgSrc, imgDst1, imgDst2;
- imgSrc = cv::imread("test.jpg");
- imgDst1 = cv::Mat(cv::Size(imgSrc.cols*2, imgSrc.rows*2), imgSrc.type(), cv::Scalar::all(0));
- imgDst2 = cv::Mat(imgDst1.size(), imgSrc.type(), cv::Scalar::all(0));
- double scale_x = (double)imgSrc.cols / imgDst1.cols;
- double scale_y = (double)imgSrc.rows / imgDst1.rows;
- uchar* dataDst = imgDst1.data;
- int stepDst = imgDst1.step;
- uchar* dataSrc = imgSrc.data;
- int stepSrc = imgSrc.step;
- int iWidthSrc = imgSrc.cols;
- int iHiehgtSrc = imgSrc.rows;
- short cbufy[2];
- float y_float, x_float;
- int y_int, x_int;
- short cbufx[2];
- for (int j=0; j<imgDst1.rows; ++j)
- {
- y_float = (float)((j + 0.5) * scale_y - 0.5);
- y_int = cvFloor(y_float);
- y_float -= y_int;
- y_int = std::min(y_int, iHiehgtSrc - 2);
- y_int = std::max(0, y_int);
- cbufy[0] = cv::saturate_cast<short>((1.f - y_float) * 2048);
- cbufy[1] = 2048 - cbufy[0];
- for (int i=0; i<imgDst1.cols; ++i)
- {
- x_float = (float)((i + 0.5) * scale_x - 0.5);
- x_int = cvFloor(x_float);
- x_float -= x_int;
- if (x_int < 0)
- {
- x_float = 0, x_int = 0;
- }
- if (x_int >= iWidthSrc - 1)
- {
- x_float = 0, x_int = iWidthSrc - 2;
- }
- cbufx[0] = cv::saturate_cast<short>((1.f - x_float) * 2048);
- cbufx[1] = 2048 - cbufx[0];
- for (int k = 0; k < imgSrc.channels(); ++k)
- {
- *(dataDst+ j*stepDst + 3*i + k) = (*(dataSrc + y_int*stepSrc + 3*x_int + k) * cbufx[0] * cbufy[0] +
- *(dataSrc + (y_int+1)*stepSrc + 3*x_int + k) * cbufx[0] * cbufy[1] +
- *(dataSrc + y_int*stepSrc + 3*(x_int+1) + k) * cbufx[1] * cbufy[0] +
- *(dataSrc + (y_int+1)*stepSrc + 3*(x_int+1) + k) * cbufx[1] * cbufy[1]) >> 22;
- }
- }
- }
- cv::imwrite("result_1.jpg", imgDst1);
- cv::resize(imgSrc, imgDst2, imgDst1.size(), 0, 0, 1);
- cv::imwrite("result_2.jpg", imgDst2);
- }
上述程式碼為3通道彩圖的縮放程式碼,圖片的載入儲存用了Opencv的結構體,其餘基本保持C的風格;
VS2008+Opencv249下面測試通過,有興趣的朋友自己試試;