获取灰度图像像素值
cv::Mat map = cv::imread("../asset/grid_map/1.png", cv::IMREAD_GRAYSCALE);
//row是行数,col是列数
int ve_stat = map.ptr<uchar>(row)[col];
dst.at<uchar>(iy, ix) = 255;
获取RGB图像像素值
image.at<Vec3b>(row, col)[0] = 0;
cv::resize(image, image_dst, cv::Size(w, h));
利用上述操作提取水平和垂直线条:
#include "stdafx.h"
#include
#include
using namespace cv;
using namespace std;
int main()
{
Mat src, dst;
src = imread("D:/images/1.png");
if (src.empty())
{
cout << "your image is null,please check your path" << endl;
return -1;
}
char INPUT_WIN[] = "input image";
char OUTPUT_WIN[] = "result image";
namedWindow(INPUT_WIN, CV_WINDOW_AUTOSIZE);
imshow(INPUT_WIN, src);
//输出灰色图像
Mat gray_src;
cvtColor(src, gray_src, CV_BGR2GRAY);
imshow("gray image", gray_src);
//装换成二值图像
Mat binImg;
adaptiveThreshold(~gray_src, binImg, 255,
ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY,15, -2);
imshow("binary image", binImg);
//水平结构元素
Mat hline = getStructuringElement(MORPH_RECT, Size(src.cols / 16, 1),
Point(-1, -1));
//垂直结构元素
Mat vline = getStructuringElement(MORPH_RECT,Size(1,src.rows/16),
Point(-1, -1));
// 矩形结构
Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3),
Point(-1, -1));
//Mat temp;
//erode(binImg, temp, kernel);
//dilate(temp,dst,kernel);
morphologyEx(binImg, dst, CV_MOP_OPEN, hline); //上面等价这个一句话
bitwise_not(dst, dst);//值取反 255 - SrcPixel
imshow("Final Result", dst);
waitKey(0);
return 0;
}
#include
#include
#include
using namespace std;
using namespace cv;
int main()
{
cv::Mat src = imread("test.png",0);
cv::Mat result = src.clone();
cv::Mat th1;
// 最大类间差法,也称大津算法
threshold(result, th1, 0, 255, THRESH_OTSU);
// 反相
th1 = 255 - th1;
// 确定连通区轮廓
std::vector<std::vector<cv::Point> > contours; // 创建轮廓容器
std::vector<cv::Vec4i> hierarchy;
cv::findContours(th1, contours, hierarchy, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_NONE, cv::Point());
// 遍历轮廓显示矩形框
for (int i = 0; i < contours.size(); ++i)
{
cv::RotatedRect rotatedrect = cv::minAreaRect(cv::Mat(contours[i]));
// 存储旋转矩形的四个点
cv::Point2f ps[4];
rotatedrect.points(ps);
std::vector<std::vector<cv::Point>> tmpContours; // 创建一个InputArrayOfArrays 类型的点集
std::vector<cv::Point> contour;
for (int i = 0; i != 4; ++i) {
contour.emplace_back(cv::Point2i(ps[i]));
}
// 插入到轮廓容器中
tmpContours.insert(tmpContours.end(), contour);
// 绘制轮廓,也就是绘制旋转矩形
drawContours(result, tmpContours, -1, Scalar(0), 1, 16); // 填充mask
}
imshow("original", src);
imshow("thresh", th1);
imshow("result", result);
waitKey(0);
return 0;
}