opencv 在圖片中找對應矩形(形狀分析)
已有一張圖片,找到裡面需要的矩形。
程式碼
#include <opencv2/opencv.hpp>
#include <opencv2/opencv_modules.hpp>
#include <opencv2/nonfree/features2d.hpp>
#include <opencv2/nonfree/nonfree.hpp>
#include <opencv2/core/Core.hpp>
#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace cv;
typedef struct
{
cv::Rect rect;
double distance;
int countInRange;
} rectPointType;
bool sortFun(const rectPointType& r1, const rectPointType& r2)
{
return (r1.countInRange > r2.countInRange);
}
int main()
{
Mat imgSrc = cv::imread("2.png", 1);
cv::Size sz = imgSrc.size();
if (sz.width > 0)
{
imshow("pic", imgSrc);
}
Mat imgBlur;
blur(imgSrc, imgBlur, cv::Size(7, 7));
Mat imgGray;
cvtColor(imgBlur, imgGray, CV_BGR2GRAY);
Mat canny_output;
int thresh = 10;
Canny(imgGray, canny_output, thresh, thresh * 2, 3);
int dilation_type = MORPH_CROSS;
int dilation_size = 2;
Mat element = getStructuringElement(dilation_type,
Size(2 * dilation_size + 1, 2 * dilation_size + 1),
Point(dilation_size, dilation_size));
dilate(canny_output, canny_output, element);
imshow("pic_canny", canny_output);
vector<vector<Point> > countours;
vector<Vec4i> hierarchy;
/// Find contours
findContours(canny_output, countours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
vector<vector<Point> > vecApproxOut;
vector<rectPointType> vecRect;
for (int i = 0; i < countours.size(); i++)
{
vector<Point> approxOut;
approxPolyDP(Mat(countours[i]), approxOut, arcLength(Mat(countours[i]), true) * 0.01, true);
if ((approxOut.size() < 4) || (approxOut.size() > 10))
{
continue;
}
double area = contourArea(approxOut);
if ((area < 1000) || (area >10000))
{
continue;
}
Rect rect;
rect = boundingRect(approxOut);
double t = ((float)rect.height / ((float)rect.width));
std::cout << "t " << t << std::endl;
if ((t < 0.8) || (t > 1.3))
{
continue;
}
rectPointType rectPoint;
rectPoint.rect = rect;
rectPoint.distance = 0.0f;
vecRect.push_back(rectPoint);
}
for (int index = 0; index < vecRect.size(); index++)
{
rectPointType now_rect = vecRect.at(index);
int countInRange = 0;
for (int i = 0; i < vecRect.size(); i++)
{
double x = now_rect.rect.x - vecRect.at(i).rect.x;
double y = now_rect.rect.y - vecRect.at(i).rect.y;
double dis = x*x + y*y;
std::cout << "index " << index << " dis " << dis << std::endl;
if (dis < 60000)
{
countInRange++;
}
}
vecRect.at(index).countInRange = countInRange;
}
std::sort(vecRect.begin(), vecRect.end(), sortFun);
vector<Point> pointBox;
for (int i = 0; i < vecRect.size(); i++)
{
if (vecRect.at(i).countInRange > 8)
{
Point p1;
Point p2;
Point p3;
Point p4;
p1.x = vecRect.at(i).rect.x;
p1.y = vecRect.at(i).rect.y;
p2.x = vecRect.at(i).rect.x + vecRect.at(i).rect.height;
p2.y = vecRect.at(i).rect.y;
p3.x = vecRect.at(i).rect.x + vecRect.at(i).rect.height;
p3.y = vecRect.at(i).rect.y + vecRect.at(i).rect.width;
p4.x = vecRect.at(i).rect.x;
p4.y = vecRect.at(i).rect.y + vecRect.at(i).rect.width;
pointBox.push_back(p1);
pointBox.push_back(p2);
pointBox.push_back(p3);
pointBox.push_back(p4);
}
}
Rect rectBox;
rectBox = boundingRect(pointBox);
line(imgSrc, Point(rectBox.x, rectBox.y), Point(rectBox.x + rectBox.width, rectBox.y), cvScalar(0, 0, 255), 4);
line(imgSrc, Point(rectBox.x + rectBox.width, rectBox.y), Point(rectBox.x + rectBox.width, rectBox.y + rectBox.height), cvScalar(0, 255, 0), 4);
line(imgSrc, Point(rectBox.x + rectBox.width, rectBox.y + rectBox.height), Point(rectBox.x, rectBox.y + rectBox.height), cvScalar(255, 0, 0), 4);
line(imgSrc, Point(rectBox.x, rectBox.y + rectBox.height), Point(rectBox.x, rectBox.y), cvScalar(0, 255, 255), 4);
imshow("imgSrc", imgSrc);
waitKey(0);
return 0;
}
結果
矩形
相關推薦
opencv 在圖片中找對應矩形(形狀分析)
已有一張圖片,找到裡面需要的矩形。 程式碼 #include <opencv2/opencv.hpp> #include <opencv2/opencv_modules.hpp> #include <opencv2/nonf
OpenCV圖片中找出你想要找的面孔
在《OpenCV訓練人臉模型並生成XML檔案》裡面講解了如何訓練人臉模型,這次就用一用訓練出來的人臉模型。 程式碼位置:24-FindActorFromPicture.py import cv2 import os img = cv2.imread('./res/zrfGrouphot
如何去除圖片中的白色背景(變透明)
很多時候,寫小程式會用到一些圖片素材,你可能會遇到這樣的問題——這些圖片,放上去之後,跟我們預期想象的不太一樣,圖片有白色的方框背景,讓自己的整個介面變得很難看。這樣的問題對於那些會修圖的大神,簡直就是小菜一碟,但不是每個搞程式的人都會修圖,那不會怎麼辦?程式設計師當然是用程式碼解決啊,而很多語
Flink 中LatencyMarks延遲監控(原始碼分析)
流式計算中處理延遲是一個非常重要的監控metric flink中通過開啟配置 metrics.latency.interval 來開啟latency後就可以在metric中看到askManagerJobMetricGroup/operator_id/operator_sub
OpenCV原始碼解析之在圖片中找四邊形-FindSquares
這個FindSquares算是比較典型的綜合技能專案吧,用到的小技巧還不少,我們先看一下幾個函式吧, 函式static double angle的作用是求角度 根據餘弦定理: 在平面座標中 通過計算變換,最後可以得到: 嗯,函式中直接用了這個結果。 其餘函式的說明 1.函
Python圖片識別找坐標(appium通過識別圖片點擊坐標)
ron 最好 screen clas shape onf tap 矩形 註意 ***如果只想了解圖片相似度識別,直接看第一步即可 ***如果想了解appium根據圖片識別點擊坐標,需要看第一、二、三步 背景|在做UI測試時,發現iOS自定義的UI控件,appium識
python-進階教程-找出字典中值最大(最小)元素的n種方法
0.摘要 字典作為儲存“鍵值對”的資料結構,往往不能直接進行計算,需要藉助額外的方法。 本文主要介紹多種方法,實現根據字典的值進行最大值、最小值和排序等計算。 1.使用dict.values()方法 dict.values()方法可以直接提取出字典的值,並存放在單獨
openCV中的findHomography函式分析以及RANSAC演算法的詳解(原始碼分析)
本文將openCV中的RANSAC程式碼全部挑選出來,進行分析和講解,以便大家更好的理解RANSAC演算法。程式碼我都試過,可以直接執行。 在計算機視覺和影象處理等很多領域,都需要用到RANSAC演算法。openCV中也有封裝好的RANSAC演算法,以便於人們使用。關於RA
Opencv中SimpleBlobDetector的使用(斑點檢測)
斑點檢測 1、斑點 斑點:是一組連通的畫素在影象中共享一些共同的屬性(如灰度值、顏色等) 2、opencv中的斑點檢測(SimpleBlobDetector) #include <opencv2/highgui.hpp> #incl
Caffe中檔案引數設定(九-1):訓練和測試自己的圖片-linux版本
在深度學習的實際應用中,我們經常用到的原始資料是圖片檔案,如jpg,jpeg,png,tif等格式的,而且有可能圖片的大小還不一致。而在caffe中經常使用的資料型別是lmdb或leveldb,因此就產生了這樣的一個問題:如何從原始圖片檔案轉換成caffe中能夠執行的db(l
用Bitmap結構儲存opencv中的單通道(8位)影象儲存為png8
需要把單通道(8位)影象儲存為png8,該影象資料已經通過opencv讀入,這裡需要藉助於Bitmap結構 bool save_png8(cv::Mat imgpng8, char* limgpath) { BITM
Java讀取圖片中的地理座標(三)
0050: FE 00 04 00 01 00 00 00 00 00 00 00 00 01 03 00 0060: 01 00 00 00 11 00 00 00 01 01 03 00 01 00 00 00 0070: 0F 00 00 00 02 01 03 00 03 00 00 00 48 0
中M2018春C入門和進階練習集-程式設計題45 7-45 找完數(20 分)
7-45 找完數(20 分) 所謂完數就是該數恰好等於除自身外的因子之和。例如:6=1+2+3,其中1、2、3為6的因子。本題要求編寫程式,找出任意兩正整數m和n之間的所有完數。 輸入格式: 輸入在一行中給出2個正整數m和n(1<m≤n≤10000),中間以空格分
數據庫中的參照完整性(Foreign Key)
part 回滾 arc bsp 元祖 varchar 指定 系統 屬性 之前在項目中遇到了這樣一個問題,我舉得簡單的樣例來說明。 比方我們有兩個表,一個表(department)存放的是部門的信息,比如部門id,部門名稱等;還有一個表是員工表(staff),員工表裏面肯
Java非遞歸的方式獲取目錄中所有文件(包括目錄)
class cto div 所有 new dir rem efi log 零、思路解析 對於給出的文件查看其下面的所有目錄,將這個目錄下的所有目錄放入待遍歷的目錄集合中,每次取出該集合中的目錄遍歷,如果是目錄再次放入該目錄中進行遍歷。 一、代碼 /**
微信公眾平臺網頁開發實戰--3.利用JSSDK在網頁中獲取地理位置(HTML5+jQuery)
fff .html 1.4 style minimum log fill rdquo 位置 復制一份JSSDK環境,創建一份index.html文件,結構如圖7.1所示。 圖7.1 7.1節文件結構 在location.js中,封裝“getLoc
查找算法(Java實現)
pac binary while n) println pub ret gin 需要 1、二分查找算法 package other; public class BinarySearch { /* * 循環實現二分查找算法arr 已排好序的數
Laravel5.5執行 npm run dev時報錯,提示cross-env找不到(not found)的解決辦法
smo font span links 命令 develop ebp amp webpack Laravel 5.4 Mix & Laravel5.5執行 npm run dev時報錯,提示cross-env找不到(not found)的解決辦法 首先
c#實現圖片二值化例子(黑白效果)
rec con devel 圖片 round amp bsp 操作 spl C#將圖片2值化示例代碼,原圖及二值化後的圖片如下: 原圖: 二值化後的圖像: 實現代碼:using System; using System.Drawing; namespace BMP2G
Office 365中的郵件跟蹤(Exchange Online)
郵件跟蹤 o365郵件跟蹤 在Office 365中調用郵件跟蹤是管理員用來監視電子郵件流的最基本的工具之一。由於電子郵件是通過Office 365的EOP傳遞的,因此有關這些郵件的信息將存儲在日誌中,並可用於管理目的。無論用戶是否刪除或清除郵件,管理員都可以查看發送和接收郵件的基本信息。