class img_u8_t
{
public:
img_u8_t(s32 w, s32 h, s32 s);
~img_u8_t();
void initBuf();
void unBuf();
void getParam(s32& w, s32& h, s32 s);
void setData(u8* d, s32 bufSize){ memcpy(data, d, bufSize * sizeof(u8)); }
u8* data;
s32 width;
s32 height;
s32 step;
};
class img_f32_t
{
public:
img_f32_t(s32 w, s32 h, s32 s);
~img_f32_t();
void initBuf();
void unBuf();
void getParam(s32& w, s32& h, s32 s);
f32* data;
s32 width;
s32 height;
s32 step;
};
img_u8_t::img_u8_t(s32 w, s32 h, s32 s) : width(w), height(h), step(s)
{
initBuf();
}
img_u8_t::~img_u8_t()
{
free(data);
data = NULL;
}
void img_u8_t::initBuf()
{
data = (u8*)malloc(width * height * sizeof(u8));
}
void img_u8_t::getParam(s32& w, s32& h, s32 s)
{
w = width;
h = height;
s = step;
}
img_f32_t::img_f32_t(s32 w, s32 h, s32 s) : width(w), height(h), step(s)
{
initBuf();
}
img_f32_t::~img_f32_t()
{
free(data);
data = NULL;
}
void img_f32_t::initBuf()
{
data = (f32*)malloc(width * height * sizeof(f32));
}
void conv(img_u8_t* src, img_f32_t* dst, img_f32_t * filter)
{
s32 srcW = src->width;
s32 srcH = src->height;
s32 srcStep = src->step;
s32 fw = filter->width;
s32 fh = filter->height;
s32 rw = (filter->width - 1) / 2;
s32 rh = (filter->height - 1) / 2;
if (fw % 2 == 0 || fh % 2 == 0)
{
cout << "the kernel size should be odd" << endl;
return;
}
u8* pSrcRow = src->data;
s32 idx = 0;
for (s32 i = rh; i < srcH - rh; i++)
{
u8* pSrcCol = pSrcRow;
for (s32 j = rw; j < srcW - rw; j++)
{
f32 sum = 0.f;
f32* filterRow = filter->data;
u8* srcBoxRow = pSrcCol;
for (s32 h = 0; h < filter->height; h++)
{
for (s32 w = 0; w < filter->width; w++)
{
sum += filterRow[w] * srcBoxRow[w];
}
srcBoxRow += src->step;
filterRow += filter->step;
}
sum = sum < 0 ? 0 : sum;
dst->data[idx] = sum;
pSrcCol++;
idx++;
}
pSrcRow += srcStep;
}
}
测试函数:
void main()
{
VideoCapture cap;
cap.open(0);
if (!cap.isOpened())
{
return;
}
Mat srcImg;
while (1)
{
cap >> srcImg;
namedWindow("src", 0);
imshow("src", srcImg);
Mat srcGray;
cvtColor(srcImg, srcGray, CV_BGR2GRAY);
img_f32_t filter(3, 3, 3);
img_u8_t convShow(filter.width, filter.height, filter.step);
convInit(&filter);
filter.data[0] = -1;
filter.data[1] = 0;
filter.data[2] = 1;
filter.data[3] = -2;
filter.data[4] = 0;
filter.data[5] = 2;
filter.data[6] = -1;
filter.data[7] = 0;
filter.data[8] = 1;
imgf2u(&filter, &convShow);
string winName = "conv_param";
showGrayImg(&convShow, winName);
blur(srcGray, srcGray, Size(5, 5));
medianBlur(srcGray, srcGray, 5);
Mat sobelImg = srcGray.clone();
Sobel(srcGray, sobelImg, srcGray.depth(), 1, 0);
namedWindow("sobel_x", 0);
imshow("sobel_x", sobelImg);
waitKey(10);
img_u8_t src(srcGray.cols, srcGray.rows, srcGray.cols);
src.setData(srcGray.data, srcGray.cols * srcGray.rows);
img_f32_t convLay1(src.width - filter.width + 1, src.height - filter.height + 1,
src.width - filter.width + 1);
conv(&src, &convLay1, &filter);
img_u8_t convLay1Show(convLay1.width, convLay1.height, convLay1.step);
imgf2u(&convLay1, &convLay1Show);
string winName1 = "convLay1";
showGrayImg(&convLay1Show, winName1);
}
cout << "hello world" << endl;
system("pause");
}