問題描述
我有以下 MATLAB 代碼,我想將其傳輸到 C++
I have the following MATLAB code which I want to transport into C++
假設 Gr
是二維矩陣并且 1/newscale == 0.5
Assume Gr
is 2d matrix and 1/newscale == 0.5
Gr = imresize(Gr, 1 / newScale);
在 MATLAB 文檔中:
B = imresize(A, scale) 返回圖像 B 的縮放倍數A. 輸入圖像 A 可以是灰度、RGB 或二值圖像.如果scale 在 0 到 1.0 之間,B 小于 A.如果 scale 更大小于 1.0,B 大于 A.
B = imresize(A, scale) returns image B that is scale times the size of A. The input image A can be a grayscale, RGB, or binary image. If scale is between 0 and 1.0, B is smaller than A. If scale is greater than 1.0, B is larger than A.
所以這意味著我將得到一個二維矩陣 == matrix_width/2 和 matrix_height/2
我如何計算這些值?根據文檔的默認值來自最近 4X4 的三次插值.
So this means I will get a 2D matrix == matrix_width/2 and matrix_height/2
How do I calculate the values? The default according to the docs are coming from cubic interpolation for nearest 4X4.
我找不到執行相同操作的 C++ 示例代碼.能否提供此類代碼的鏈接?
I can't find a sample code for C++ that does the same. Can you please provide a link to such code?
我還發現 這個 OpenCV 函數,resize
.
它和 MATLAB 的功能一樣嗎?
Does it do the same as the MATLAB one?
推薦答案
是的,請注意 MATLAB 的 imresize
有 默認啟用抗鋸齒:
Yes, just be aware that MATLAB's imresize
has anti-aliasing enabled by default:
imresize(A,scale,'bilinear')
對比你會用 cv::resize()
得到什么,它沒有抗鋸齒:
vs. what you would get with cv::resize()
, which does not have anti-aliasing:
imresize(A,scale,'bilinear','AntiAliasing',false)
正如 Amro 提到的,MATLAB 中的默認值是 bicubic
,所以一定要指定.
And as Amro mentioned, the default in MATLAB is bicubic
, so be sure to specify.
無需修改代碼即可通過雙線性插值獲得匹配結果.
No code modifications are necessary to get matching results with bilinear interpolation.
示例 OpenCV 代碼段:
Example OpenCV snippet:
cv::Mat src(4, 4, CV_32F);
for (int i = 0; i < 16; ++i)
src.at<float>(i) = i;
std::cout << src << std::endl;
cv::Mat dst;
cv::resize(src, dst, Size(0, 0), 0.5, 0.5, INTER_LINEAR);
std::cout << dst << std::endl;
輸出 (OpenCV)
[0, 1, 2, 3;
4, 5, 6, 7;
8, 9, 10, 11;
12, 13, 14, 15]
[2.5, 4.5;
10.5, 12.5]
MATLAB
>> M = reshape(0:15,4,4).';
>> imresize(M,0.5,'bilinear','AntiAliasing',true)
ans =
3.125 4.875
10.125 11.875
>> imresize(M,0.5,'bilinear','AntiAliasing',false)
ans =
2.5 4.5
10.5 12.5
請注意,關閉抗鋸齒后的結果是一樣的.
Note that the results are the same with anti-aliasing turned off.
然而,在'bicubic'
和INTER_CUBIC
之間,由于權重方案的原因,結果是不同的!參見此處,了解數學差異的詳細信息.問題出在計算三次插值系數的 interpolateCubic()
函數中,其中使用常數 a = -0.75
而不是 a = -0.5
就像在 MATLAB 中一樣.但是,如果您編輯 imgwarp.cpp 并更改代碼:
However, between 'bicubic'
and INTER_CUBIC
, the results are different on account of the weighting scheme! See here for details on the mathematical difference. The issue is in the interpolateCubic()
function that computes the cubic interpolant's coefficients, where a constant of a = -0.75
is used rather than a = -0.5
like in MATLAB. However, if you edit imgwarp.cpp and change the code :
static inline void interpolateCubic( float x, float* coeffs )
{
const float A = -0.75f;
...
到:
static inline void interpolateCubic( float x, float* coeffs )
{
const float A = -0.50f;
...
并重建 OpenCV(提示:禁用 CUDA 和 gpu 模塊以縮短編譯時間),然后您會得到相同的結果:
and rebuild OpenCV (tip: disable CUDA and the gpu module for short compile time), then you get the same results:
MATLAB
>> imresize(M,0.5,'bicubic','AntiAliasing',false)
ans =
2.1875 4.3125
10.6875 12.8125
OpenCV
[0, 1, 2, 3;
4, 5, 6, 7;
8, 9, 10, 11;
12, 13, 14, 15]
[2.1875, 4.3125;
10.6875, 12.8125]
更多關于立方這里.
這篇關于MATLAB vs C++ vs OpenCV - imresize的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!