基于opencv的信用卡号切割(c++)(一)

博客围绕基于OpenCV的信用卡号切割展开,使用C++语言实现。介绍了切割目标、原理图并给出了源码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

基于opencv的信用卡号切割(c++)

目标:
在这里插入图片描述
在这里插入图片描述
原理图:
在这里插入图片描述

源码:

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;


int main(int argc, char** argv) {

	Mat src = imread("E:/opencv/card_1 .jpg");
	//imshow("src",src);

	//灰度化
	Mat gray;
	cvtColor(src, gray, COLOR_BGR2GRAY);
	//imshow("gray", gray);

	//二值化
	Mat binary;
	threshold(gray, binary, 75, 255, THRESH_BINARY);
	imshow("binary", binary);
	



	Mat dst;
	Mat element = getStructuringElement(MORPH_RECT, Size(9, 1), Point(-1, -1));
	Mat element1 = getStructuringElement(MORPH_RECT, Size(9, 1), Point(-1, -1));
	
	morphologyEx(~binary, dst, MORPH_DILATE, element);
	//imshow("dst", dst);
	morphologyEx(dst, dst, MORPH_ERODE, element1);
	//imshow("dst1", dst);
	
	

	
	//提取轮廓
	vector<vector<Point>> contours;
	Rect rect;
	findContours(dst, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);

	Mat out_image = Mat::zeros(gray.size(), gray.type());
	//轮廓分析
	for (size_t i = 0; i < contours.size(); i++)
	{
		
		double length = arcLength(contours[i], true);
		double area = contourArea(contours[i]);

		

		if (length < 130) {
			continue;
		}
		if (area<800)
		{
			continue;
		}
		cout << "周长 length:" << length << endl;
		cout << "面积 area:" << area << endl;

		rect = boundingRect(contours[i]);
	
		
		cout << "rect::" << rect << endl;
		cout << "圆点 x:" << rect.x << endl;
		cout << "圆点 y:" << rect.y << endl;
		drawContours(out_image, contours, i, Scalar(255), 2, 8);

		
	}
  // imshow("out_image", out_image);

   


   Mat imageROI;
   imageROI = src(Range((rect.y - 5), (rect.y+rect.height + 5)),Range((rect.x - 5), (rect.x +rect.width + 5)) );
   imshow("imageROI", imageROI);
   rectangle(src, rect, Scalar(0, 0, 255), 1, 8, 0);
   imshow("result", src);



	waitKey(0);
	return 0;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值