试图创建我自己的图像过滤器代码?
trying to create my own image filter code?
为了让我的图像处理基础知识更加清晰,我正在尝试编写代码来制作图像过滤器,该过滤器采用内核并将其卷积到整个图像。
简而言之,我正在尝试复制 opencv 的 filter2d 函数,但是它给了我一些意想不到的结果。
我的代码:
#include <QCoreApplication>
#include <opencv2/opencv.hpp>
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"'
#include<stdlib.h>
using namespace cv;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Point anchor = Point(-1,-1);
Mat kernel = (Mat_<float>(3,3))<<(1,1,1,1,-8,1,1,1,1);//for gaussianblur
/*Mat kernel_inverse(kernel.size(),kernel.type());
for(int i=0;i<kernel_inverse.rows;i++)
for(int j=0;j<kernel_inverse.cols;j++)
{
kernel_inverse.at<uchar>(i,j)=kernel.at<uchar>(kernel.rows-i-1,kernel.cols-j-1);
}*/
Mat src = imread("/home/vasu/SENSOVISION/Filter Testing/Elon Musk 8.jpg",0);
imshow("source",src);
Mat result(src.size(),src.type());
//Mat result_inverse(src.size(),src.type());
for(int i=1;i< src.rows;i++)
for(int j=1;j<src.cols;j++)
{
result.at<uchar>(i,j)=((src.at<uchar>(i-1,j-1)*kernel.at<uchar>(0,0))+(src.at<uchar>(i-1,j)*kernel.at<uchar>(0,1))+(src.at<uchar>(i-1,j+1)*kernel.at<uchar>(0,2))+(src.at<uchar>(i,j-1)*kernel.at<uchar>(1,0))+(src.at<uchar>(i,j)*kernel.at<uchar>(1,1))+(src.at<uchar>(i,j+1)*kernel.at<uchar>(1,2))+(src.at<uchar>(i+1,j-1)*kernel.at<uchar>(2,0))+(src.at<uchar>(i+1,j)*kernel.at<uchar>(2,1))+(src.at<uchar>(i+1,j+1)*kernel.at<uchar>(2,2)));
//result_inverse.at<uchar>(i,j)=((src.at<uchar>(i-1,j-1)*kernel_inverse.at<uchar>(0,0))+(src.at<uchar>(i-1,j)*kernel_inverse.at<uchar>(0,1))+(src.at<uchar>(i-1,j+1)*kernel_inverse.at<uchar>(0,2))+(src.at<uchar>(i,j-1)*kernel_inverse.at<uchar>(1,0))+(src.at<uchar>(i,j)*kernel_inverse.at<uchar>(1,1))+(src.at<uchar>(i,j+1)*kernel_inverse.at<uchar>(1,2))+(src.at<uchar>(i+1,j-1)*kernel_inverse.at<uchar>(2,0))+(src.at<uchar>(i+1,j)*kernel_inverse.at<uchar>(2,1))+(src.at<uchar>(i+1,j+1)*kernel_inverse.at<uchar>(2,2)));
}
imshow("result",result);
//imshow("result_inverse",result_inverse);
return a.exec();
}
通过这段代码我得到
有人能告诉我我做错了什么吗?
您以 unsigned char 格式进行计算,将产生不正确的结果。
在浮点数中进行计算,然后将结果放入结果图像。
为了让我的图像处理基础知识更加清晰,我正在尝试编写代码来制作图像过滤器,该过滤器采用内核并将其卷积到整个图像。
简而言之,我正在尝试复制 opencv 的 filter2d 函数,但是它给了我一些意想不到的结果。
我的代码:
#include <QCoreApplication>
#include <opencv2/opencv.hpp>
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"'
#include<stdlib.h>
using namespace cv;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Point anchor = Point(-1,-1);
Mat kernel = (Mat_<float>(3,3))<<(1,1,1,1,-8,1,1,1,1);//for gaussianblur
/*Mat kernel_inverse(kernel.size(),kernel.type());
for(int i=0;i<kernel_inverse.rows;i++)
for(int j=0;j<kernel_inverse.cols;j++)
{
kernel_inverse.at<uchar>(i,j)=kernel.at<uchar>(kernel.rows-i-1,kernel.cols-j-1);
}*/
Mat src = imread("/home/vasu/SENSOVISION/Filter Testing/Elon Musk 8.jpg",0);
imshow("source",src);
Mat result(src.size(),src.type());
//Mat result_inverse(src.size(),src.type());
for(int i=1;i< src.rows;i++)
for(int j=1;j<src.cols;j++)
{
result.at<uchar>(i,j)=((src.at<uchar>(i-1,j-1)*kernel.at<uchar>(0,0))+(src.at<uchar>(i-1,j)*kernel.at<uchar>(0,1))+(src.at<uchar>(i-1,j+1)*kernel.at<uchar>(0,2))+(src.at<uchar>(i,j-1)*kernel.at<uchar>(1,0))+(src.at<uchar>(i,j)*kernel.at<uchar>(1,1))+(src.at<uchar>(i,j+1)*kernel.at<uchar>(1,2))+(src.at<uchar>(i+1,j-1)*kernel.at<uchar>(2,0))+(src.at<uchar>(i+1,j)*kernel.at<uchar>(2,1))+(src.at<uchar>(i+1,j+1)*kernel.at<uchar>(2,2)));
//result_inverse.at<uchar>(i,j)=((src.at<uchar>(i-1,j-1)*kernel_inverse.at<uchar>(0,0))+(src.at<uchar>(i-1,j)*kernel_inverse.at<uchar>(0,1))+(src.at<uchar>(i-1,j+1)*kernel_inverse.at<uchar>(0,2))+(src.at<uchar>(i,j-1)*kernel_inverse.at<uchar>(1,0))+(src.at<uchar>(i,j)*kernel_inverse.at<uchar>(1,1))+(src.at<uchar>(i,j+1)*kernel_inverse.at<uchar>(1,2))+(src.at<uchar>(i+1,j-1)*kernel_inverse.at<uchar>(2,0))+(src.at<uchar>(i+1,j)*kernel_inverse.at<uchar>(2,1))+(src.at<uchar>(i+1,j+1)*kernel_inverse.at<uchar>(2,2)));
}
imshow("result",result);
//imshow("result_inverse",result_inverse);
return a.exec();
}
通过这段代码我得到
有人能告诉我我做错了什么吗?
您以 unsigned char 格式进行计算,将产生不正确的结果。 在浮点数中进行计算,然后将结果放入结果图像。