//filter2d的卷积方法
printf( "filter2d的卷积方法\n");
Mat srcMat(,,CV_32F);
Mat dstMat(,,CV_32F);
Mat srcH(,,CV_32F);
srcH.at<float>(,) = -;
srcH.at<float>(,) = -;
srcH.at<float>(,) = ;
srcH.at<float>(,) = ;
srcH.at<float>(,) = ;
srcH.at<float>(,) = ;
srcH.at<float>(,) = ;
srcH.at<float>(,) = ;
srcH.at<float>(,) = ;
printf( "卷积核\n");
for (int i=;i<srcH.rows;i++){
for (int j=;j<srcH.cols;j++){
printf("%f ",srcH.at<float>(i,j) );
}
printf("\n");
}
printf( "输入\n");
for (int i = ; i < ; i++){
for (int j = ; j < ; j++)
srcMat.at<float>(i,j) = i+;
}
for (int i = ; i < ; i++){
for (int j = ; j < ; j++){
printf("%.1f ",srcMat.at<float>(i,j));
}
printf("\n");
}
printf( "输出\n");
filter2D(srcMat,dstMat,srcMat.depth(),srcH);
printf("\n"); printf("\n");
for (int i = ; i < ; i++){
for (int j = ; j < ; j++){
printf("%.1f ",dstMat.at<float>(i,j));
}
printf("\n");
}
waitKey();
return ;
}
imwrite("gray.jpg",gray);
gray.convertTo(gray,CV_32F);
Mat dst;
filter2D(gray,dst,gray.depth(),srcH);
for (int i = ; i < gray.rows; i++){
for (int j = ; j < gray.cols; j++){
printf("%f ",dst.at<float>(i,j));
}
printf("\n");
}
#include <iostream>
#include <fstream>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace std;
using namespace cv;
int _tmain(int argc, _TCHAR* argv[])
{
////filter2d的卷积方法
printf( "filter2d的卷积方法\n");
//Mat srcMat(10,10,CV_32F);
//Mat dstMat(10,10,CV_32F);
Mat srcH(,,CV_32F);
srcH.at<float>(,) = -;
srcH.at<float>(,) = -;
srcH.at<float>(,) = ;
srcH.at<float>(,) = ;
srcH.at<float>(,) = ;
srcH.at<float>(,) = ;
srcH.at<float>(,) = ;
srcH.at<float>(,) = ;
srcH.at<float>(,) = ;
printf( "卷积核\n");
for (int i=;i<srcH.rows;i++){
for (int j=;j<srcH.cols;j++){
printf("%f ",srcH.at<float>(i,j) );
}
printf("\n");
}
//读取图片的处理的方法
Mat gray = imread("test.jpg",);
imwrite("gray.jpg",gray);
gray.convertTo(gray,CV_32F);
gray = gray/; //归一化处理
fstream ftxt;
ftxt.open("src.txt",ios::out); //写入的方式,同时是append模式的就不会覆盖掉前面的东西了。
for (int i = ; i < gray.rows; i++){
for (int j = ; j < gray.cols; j++){
ftxt<<gray.at<float>(i,j)<<" ";
}
ftxt<<endl;
}
ftxt.close();
Mat dst;
filter2D(gray,dst,gray.depth(),srcH);
ftxt.open("rst.txt",ios::out); //写入的方式,同时是append模式的就不会覆盖掉前面的东西了。
for (int i = ; i < gray.rows; i++){
for (int j = ; j < gray.cols; j++){
ftxt<<dst.at<float>(i,j)<<" ";
}
ftxt<<endl;
}
ftxt.close();
imshow("dst",dst);
waitKey();
return ;
}
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace std;
using namespace cv; int _tmain(int argc, _TCHAR* argv[])
{
////filter2d的卷积方法
printf( "filter2d的卷积方法\n");
//Mat srcMat(10,10,CV_32F);
//Mat dstMat(10,10,CV_32F);
Mat srcH(3,3,CV_32F);
srcH.at<float>(0,0) = -2;
srcH.at<float>(0,1) = -1;
srcH.at<float>(0,2) = 4;
srcH.at<float>(1,0) = 3;
srcH.at<float>(1,1) = 3;
srcH.at<float>(1,2) = 3;
srcH.at<float>(2,0) = 3;
srcH.at<float>(2,1) = 2;
srcH.at<float>(2,2) = 1;
printf( "卷积核\n");
for (int i=0;i<srcH.rows;i++){
for (int j=0;j<srcH.cols;j++){
printf("%f ",srcH.at<float>(i,j) );
}
printf("\n");
}
//printf( "输入\n");
//for (int i = 0; i < 10; i++){
// for (int j = 0; j < 10; j++)
// srcMat.at<float>(i,j) = i+1;
//}
//for (int i = 0; i < 10; i++){
// for (int j = 0; j < 10; j++){
// printf("%.1f ",srcMat.at<float>(i,j));
// }
// printf("\n");
//}
//printf( "输出\n");
//filter2D(srcMat,dstMat,srcMat.depth(),srcH);
//printf("\n"); printf("\n");
//for (int i = 0; i < 10; i++){
// for (int j = 0; j < 10; j++){
// printf("%.1f ",dstMat.at<float>(i,j));
// }
// printf("\n");
//}
//读取图片的处理的方法
Mat gray = imread("test.jpg",0);
imwrite("gray.jpg",gray);
gray.convertTo(gray,CV_32F);
gray = gray/255; //归一化处理
fstream ftxt;
ftxt.open("src.txt",ios::out); //写入的方式,同时是append模式的就不会覆盖掉前面的东西了。
for (int i = 0; i < gray.rows; i++){
for (int j = 0; j < gray.cols; j++){
ftxt<<gray.at<float>(i,j)<<" ";
//printf("%.1f ",dst.at<float>(i,j));
}
ftxt<<endl;
//printf("\n");
}
ftxt.close();
Mat dst;
filter2D(gray,dst,gray.depth(),srcH); ftxt.open("rst.txt",ios::out); //写入的方式,同时是append模式的就不会覆盖掉前面的东西了。
for (int i = 0; i < gray.rows; i++){
for (int j = 0; j < gray.cols; j++){
ftxt<<dst.at<float>(i,j)<<" ";
//printf("%.1f ",dst.at<float>(i,j));
}
ftxt<<endl;
//printf("\n");
}
ftxt.close();
imshow("dst",dst); waitKey();
return 0;
}
p.s 转一篇有用博文,时间久了原始链接已经丢失,抱歉
1
2 3 4 5 6 7 8 |
clc;
clear; BW = im2bw( imread('imfilltest.tif')); imshow(BW); holes = imfill(BW, 'holes'); BW(~holes) = 1; |
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
#include
#include #include using namespace std; void my_imfillholes(Mat &src) void test_my_imfillholes() void main() |
1 1 1 0 0 0 0 0
1 1 1 0 1 1 0 0
1 1 1 0 1 1 0 0
1 1 1 0 1 1 1 0
1 1 1 0 1 1 1 0
1 1 1 0 1 1 1 0
1 1 1 0 0 1 1 0
1 1 1 0 0 0 0 0
0 0 0 0 0 0 0 0
0 1 1 0 1 1 0 0
0 1 1 0 1 1 0 0
0 1 1 0 1 1 1 0
0 1 1 0 1 1 1 0
0 1 1 0 1 1 1 0
0 1 1 0 0 1 1 0
0 0 0 0 0 0 0 0
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
#include
#include #include using namespace std; void my_imfillholes_v2() //setp 2: find the contour fill holes void main() |