如何在OpenCV中使用Matlab的512元素查找表数组? [英] How to use Matlab's 512 element lookup table array in OpenCV?
问题描述
我正在OpenCV中设计形态运算.我试图模仿Matlab的bwmorph中的函数remove
和bridge
.为此,我参考了bwmorph.m的函数定义,在那里获得了用于删除和桥接的查找表数组.
I am designing morphological operations in OpenCV. I am trying to mimic the functions remove
and bridge
in Matlab's bwmorph. To do this I referred to the function definition of bwmorph.m, there I obtained the Look up table arrays for remove and bridge.
在此步骤之后,Matlab和OpenCV的过程都是相同的.
After that step the procedure is same for both Matlab and OpenCV.
lut(img,lutarray,img)
问题是Matlab使用512元素(9位)查找表方案,而OpenCV使用256元素(8位)查找方案,我该如何在OpenCV中使用Matlab lutarray?
Problem is that Matlab uses a 512 element (9bit) look up table scheme while OpenCV uses a 256 element (8bit) look up scheme, how do I use the Matlab lutarray in OpenCV?
After doing some research I came across this post.
当人们说他们将图像从0-512拆分"为两部分时,这是什么意思?
What does the person mean when they're saying that they "split" the image from 0-512 and then into two parts?
以上方法是否正确?有其他替代方法吗?
Is the above method even correct? Are there any alternates to doing this?
推荐答案
bwlookup(bw,lut)
http://se.mathworks.com/help/images/ref/bwlookup.html
或内部,applylut
都对二进制(黑白)图像执行2×2或3×3邻域运算,而OpenCV的cv::LUT
执行每个像素的灰度转换(紧密与MATLAB中的intlut
相关).后者的一个示例是对灰度图像执行伽玛校正.
or internally, applylut
both perform a 2-by-2 or 3-by-3 neighborhood operation on a binary (black & white) image, whereas OpenCV's cv::LUT
performs a per pixel gray level transform (closely related to intlut
in MATLAB). An example of latter is performing a gamma correction on gray level image.
//! transforms array of numbers using a lookup table: dst(i)=lut(src(i))
CV_EXPORTS_W void LUT(InputArray src, InputArray lut, OutputArray dst,
int interpolation=0);
据我所知,OpenCV中没有邻域bwlookup
实现.但是,按照MATLAB bwlookup
的描述,您可以自己编写.
To my knowledge, there is no neighborhood bwlookup
implementation in OpenCV. However, following the description of MATLAB's bwlookup
, you can write it yourself.
// performs 3-by-3 lookup on binary image
void bwlookup(
const cv::Mat & in,
cv::Mat & out,
const cv::Mat & lut,
int bordertype=cv::BORDER_CONSTANT,
cv::Scalar px = cv::Scalar(0) )
{
if ( in.type() != CV_8UC1 )
CV_Error(CV_StsError, "er");
if ( lut.type() != CV_8UC1 || lut.rows*lut.cols!=512 || !lut.isContinuous() )
CV_Error(CV_StsError, "lut size != 512" );
if ( out.type() != in.type() || out.size() != in.size() )
out = cv::Mat( in.size(), in.type() );
const unsigned char * _lut = lut.data;
cv::Mat t;
cv::copyMakeBorder( in,t,1,1,1,1,bordertype,px);
const int rows=in.rows+1;
const int cols=in.cols+1;
for ( int y=1;y<rows;++y)
{
for ( int x=1;x<cols;++x)
{
int L = 0;
const int jmax=y+1;
#if 0 // row-major order
for ( int j=y-1, k=1; j<=jmax; ++j, k<<=3 )
{
const unsigned char * p = t.ptr<unsigned char>(j) + x-1;
for ( unsigned int u=0;u<3;++u )
{
if ( p[u] )
L += (k<<u);
#else // column-major order (MATLAB)
for ( int j=y-1, k=1; j<=jmax; ++j, k<<=1 )
{
const unsigned char * p = t.ptr<unsigned char>(j) + x-1;
for ( unsigned int u=0;u<3;++u )
{
if ( p[u] )
L += (k<<3*u);
#endif
}
}
out.at<unsigned char>(y-1,x-1)=_lut[ L ];
}
}
}
我针对remove
和bridge
测试了它,因此应该可以工作.希望有帮助.
I tested it against remove
and bridge
so should work. Hope that helps.
根据随机查找表进行检查后,
After checking against a random lookup table,
lut = uint8( rand(512,1)>0.5 ); % @MATLAB
B = bwlookup( A, lut );
我颠倒了索引在查找表中出现的顺序(与操作是否对称无关).
I flipped the order the indices appear in the lookup table (doesn't matter if the operation is symmetric).
这篇关于如何在OpenCV中使用Matlab的512元素查找表数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!