国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

C# OpenCvSharp 圖像校正

這篇具有很好參考價值的文章主要介紹了C# OpenCvSharp 圖像校正。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

目錄

效果

代碼

下載


效果

opencvsharp 矩形檢測,OpenCV,C#,c#,opencv,計算機(jī)視覺,C# 圖像校正

代碼

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OpenCvSharp;
using OpenCvSharp.Extensions;

namespace OpenCvSharp_圖像校正
{
? ? public partial class Form1 : Form
? ? {
? ? ? ? public Form1()
? ? ? ? {
? ? ? ? ? ? InitializeComponent();
? ? ? ? }

? ? ? ? string img = "test.png";

? ? ? ? private void Form1_Load(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? pictureBox1.Image = new Bitmap(img);
? ? ? ? }

? ? ? ? private void button1_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? Mat src = new Mat(img);

? ? ? ? ? ? //轉(zhuǎn)化為灰度圖
? ? ? ? ? ? //Cv2.CvtColor(src, src, ColorConversionCodes.RGB2GRAY);

? ? ? ? ? ? InputArray kernel = Cv2.GetStructuringElement(MorphShapes.Rect, new OpenCvSharp.Size(3, 3));
? ? ? ? ? ? Cv2.MorphologyEx(src, src, MorphTypes.Close, kernel, new OpenCvSharp.Point(-1, -1), 3);
? ? ? ? ? ? //Cv2.ImShow("MorphologyEx", src);

? ? ? ? ? ? /*
? ? ? ? ? ? ? ? ksize,高斯內(nèi)核大小,ksize.width和ksize.height必須是正奇數(shù),兩者可以不相同,值越大越模糊
? ? ? ? ? ? ? ? sigmaX,Y軸方向的標(biāo)準(zhǔn)差,值越大越模糊
? ? ? ? ? ? ? ? sigmaY,X軸方向的標(biāo)準(zhǔn)差,值越大越模糊
? ? ? ? ? ? ?*/
? ? ? ? ? ? Cv2.GaussianBlur(src, src, new OpenCvSharp.Size(11, 11), 2, 2);
? ? ? ? ? ? //Cv2.ImShow("GaussianBlur", src);

? ? ? ? ? ? //Canny邊緣檢測
? ? ? ? ? ? Mat canny_Image = new Mat();
? ? ? ? ? ? Cv2.Canny(src, canny_Image, 10, 30, 3, false);

? ? ? ? ? ? OpenCvSharp.Point[][] contours;
? ? ? ? ? ? HierarchyIndex[] hierarchly;
? ? ? ? ? ? /*
?? ? ? ? ? ? ?findContours找到輪廓
?? ? ? ? ? ? ?第一個參數(shù):單通道圖像矩陣,可以是灰度圖,但更常用的是二值圖像,一般是經(jīng)過Canny、拉普拉斯等邊緣檢測算子處理過的二值圖像;
?? ? ? ? ? ? ?第二個參數(shù):contours?
?? ? ? ? ? ? ?第三個參數(shù):hierarchy
?? ? ? ? ? ? ?第四個參數(shù):輪廓的檢索模式
?? ? ??? ??? ? ? ? ? ?取值一:CV_RETR_EXTERNAL 只檢測最外圍輪廓,包含在外圍輪廓內(nèi)的內(nèi)圍輪廓被忽略
?? ? ??? ??? ? ? ? ? ?取值二:CV_RETR_LIST ? ? 檢測所有的輪廓,包括內(nèi)圍、外圍輪廓,但是檢測到的輪廓不建立等級關(guān)系,彼此之間獨立,沒有等級關(guān)系,這就意味著這個檢索模式下不存在父輪廓或內(nèi)嵌輪廓,所以hierarchy向量內(nèi)所有元素的第3、第4個分量都會被置為-1,具體下文會講到
?? ? ??? ??? ? ? ? ? ?取值三:CV_RETR_CCOMP ? ?檢測所有的輪廓,但所有輪廓只建立兩個等級關(guān)系,外圍為頂層,若外圍內(nèi)的內(nèi)圍輪廓還包含了其他的輪廓信息,則內(nèi)圍內(nèi)的所有輪廓均歸屬于頂層
?? ? ??? ??? ? ? ? ? ?取值四:CV_RETR_TREE ? ? 檢測所有輪廓,所有輪廓建立一個等級樹結(jié)構(gòu)。外層輪廓包含內(nèi)層輪廓,內(nèi)層輪廓還可以繼續(xù)包含內(nèi)嵌輪廓。
?? ? ? ? ? ? ?第五個參數(shù):輪廓的近似方法
?? ? ??? ??? ? ? ? ? ?取值一:CV_CHAIN_APPROX_NONE ? 保存物體邊界上所有連續(xù)的輪廓點到contours向量內(nèi)
?? ? ??? ??? ? ? ? ? ?取值二:CV_CHAIN_APPROX_SIMPLE 僅保存輪廓的拐點信息,把所有輪廓拐點處的點保存入contours向量內(nèi),拐點與拐點之間直線段上的信息點不予保留
?? ? ??? ??? ? ? ? ? ?取值三和四:CV_CHAIN_APPROX_TC89_L1,CV_CHAIN_APPROX_TC89_KCOS使用teh-Chinl chain 近似算法
?? ? ? ? ? ? ?第六個參數(shù):Point偏移量,所有的輪廓信息相對于原始圖像對應(yīng)點的偏移量,相當(dāng)于在每一個檢測出的輪廓點上加上該偏移量,且Point可以是負(fù)值。不填為默認(rèn)不偏移Point()
?? ? ? ? ? ? */
? ? ? ? ? ? Cv2.FindContours(canny_Image, out contours, out hierarchly,
? ? ? ? ? ? ? ? RetrievalModes.External,
? ? ? ? ? ? ? ? ContourApproximationModes.ApproxSimple,
? ? ? ? ? ? ? ? new OpenCvSharp.Point(0, 0));

? ? ? ? ? ? if (contours.Length == 0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? MessageBox.Show("邊緣檢測失敗");
? ? ? ? ? ? ? ? return;
? ? ? ? ? ? }

? ? ? ? ? ? Random rnd = new Random();
? ? ? ? ? ? Scalar color;
? ? ? ? ? ? color = new Scalar(0, 255, 0);
? ? ? ? ? ? for (int i = 0; i < contours.Length; i++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? color = new Scalar(rnd.Next(0, 255), rnd.Next(0, 255), rnd.Next(0, 255));
? ? ? ? ? ? ? ? Cv2.DrawContours(src, contours, i, color, 2, LineTypes.Link4);
? ? ? ? ? ? }
? ? ? ? ? ? //Cv2.ImShow("contours", src);

? ? ? ? ? ? //求出面積最大的輪廓
? ? ? ? ? ? double max_area = 0.0;
? ? ? ? ? ? double currentArea = 0.0;
? ? ? ? ? ? OpenCvSharp.Point[] max_contour = null;
? ? ? ? ? ? for (int i = 0; i < contours.Length; i++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? currentArea = Cv2.ContourArea(contours[i]);
? ? ? ? ? ? ? ? if (currentArea > max_area)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? max_area = currentArea;
? ? ? ? ? ? ? ? ? ? max_contour = contours[i];
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }

? ? ? ? ? ? //多邊形擬合凸包的四個頂點
? ? ? ? ? ? OpenCvSharp.Point[] hull = Cv2.ConvexHull(max_contour);
? ? ? ? ? ? double epsilon = 0.02 * Cv2.ArcLength(max_contour, true);
? ? ? ? ? ? OpenCvSharp.Point[] approx = Cv2.ApproxPolyDP(hull, epsilon, true);

? ? ? ? ? ? if (approx.Length != 4)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? MessageBox.Show("擬合凸包的四個頂點失敗");
? ? ? ? ? ? ? ? return;
? ? ? ? ? ? }

? ? ? ? ? ? Scalar scalar2 = new Scalar(0, 255, 255);

? ? ? ? ? ? Cv2.Line(src, approx[0], approx[1], scalar2, 1, LineTypes.Link4);
? ? ? ? ? ? Cv2.Line(src, approx[1], approx[2], scalar2, 1, LineTypes.Link4);
? ? ? ? ? ? Cv2.Line(src, approx[2], approx[3], scalar2, 1, LineTypes.Link4);
? ? ? ? ? ? Cv2.Line(src, approx[3], approx[0], scalar2, 1, LineTypes.Link4);

? ? ? ? ? ? //排序
? ? ? ? ? ? Array.Sort(approx, (cs1, cs2) =>
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if (cs1 != null && cs1 != null)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? if (cs1.Y > cs2.Y)
? ? ? ? ? ? ? ? ? ? ? ? return 1;
? ? ? ? ? ? ? ? ? ? else if (cs1.Y == cs2.Y)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? if (cs1.X < cs2.X)
? ? ? ? ? ? ? ? ? ? ? ? ? ? return 1;
? ? ? ? ? ? ? ? ? ? ? ? else return -1;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? ? ? ? ? return -1;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? return 0;

? ? ? ? ? ? });

? ? ? ? ? ? //算法找出的角點
? ? ? ? ? ? OpenCvSharp.Point2f[] srcPt = new OpenCvSharp.Point2f[4];
? ? ? ? ? ? srcPt[0] = approx[0];
? ? ? ? ? ? srcPt[1] = approx[1];
? ? ? ? ? ? srcPt[2] = approx[3];
? ? ? ? ? ? srcPt[3] = approx[2];

? ? ? ? ? ? //最小外接矩形
? ? ? ? ? ? RotatedRect rect = Cv2.MinAreaRect(srcPt);
? ? ? ? ? ? Rect box = rect.BoundingRect();
? ? ? ? ? ? OpenCvSharp.Point2f[] dstPt = new OpenCvSharp.Point2f[4];

? ? ? ? ? ? dstPt[0].X = box.X;
? ? ? ? ? ? dstPt[0].Y = box.Y;

? ? ? ? ? ? dstPt[1].X = box.X + box.Width;
? ? ? ? ? ? dstPt[1].Y = box.Y;

? ? ? ? ? ? dstPt[2].X = box.X + box.Width;
? ? ? ? ? ? dstPt[2].Y = box.Y + box.Height;

? ? ? ? ? ? dstPt[3].X = box.X;
? ? ? ? ? ? dstPt[3].Y = box.Y + box.Height;

? ? ? ? ? ? Mat src2 = new Mat(img);
? ? ? ? ? ? Mat final = new Mat();
? ? ? ? ? ? Mat warpmatrix = Cv2.GetPerspectiveTransform(srcPt, dstPt);//獲得變換矩陣
? ? ? ? ? ? Cv2.WarpPerspective(src2, final, warpmatrix, src.Size());//投射變換,將結(jié)果賦給final

? ? ? ? ? ? Bitmap temp = BitmapConverter.ToBitmap(final);

? ? ? ? ? ? pictureBox2.Image = temp;

? ? ? ? ? ? DrawLine(srcPt, dstPt);

? ? ? ? ? ? //Application.DoEvents();
? ? ? ? ? ? //System.Threading.Thread.Sleep(1000);
? ? ? ? ? ? //pictureBox2.Image = CutImage(temp, (int)p2f[0].X, (int)p2f[0].Y, (int)p2f[2].X, (int)p2f[2].Y);

? ? ? ? }

? ? ? ? void DrawLine(OpenCvSharp.Point2f[] srcPt, OpenCvSharp.Point2f[] dstPt)
? ? ? ? {
? ? ? ? ? ? Bitmap bmp = new Bitmap(img);
? ? ? ? ? ? Graphics g = Graphics.FromImage(bmp);

? ? ? ? ? ? Pen pen = new Pen(Color.Red, 3);
? ? ? ? ? ? Pen pen2 = new Pen(Color.Blue, 3);

? ? ? ? ? ? g.DrawLine(pen, srcPt[0].X, srcPt[0].Y, srcPt[1].X, srcPt[1].Y);
? ? ? ? ? ? g.DrawLine(pen, srcPt[1].X, srcPt[1].Y, srcPt[2].X, srcPt[2].Y);
? ? ? ? ? ? g.DrawLine(pen, srcPt[2].X, srcPt[2].Y, srcPt[3].X, srcPt[3].Y);
? ? ? ? ? ? g.DrawLine(pen, srcPt[3].X, srcPt[3].Y, srcPt[0].X, srcPt[0].Y);

? ? ? ? ? ? g.DrawLine(pen2, dstPt[0].X, dstPt[0].Y, dstPt[1].X, dstPt[1].Y);
? ? ? ? ? ? g.DrawLine(pen2, dstPt[1].X, dstPt[1].Y, dstPt[2].X, dstPt[2].Y);
? ? ? ? ? ? g.DrawLine(pen2, dstPt[2].X, dstPt[2].Y, dstPt[3].X, dstPt[3].Y);
? ? ? ? ? ? g.DrawLine(pen2, dstPt[3].X, dstPt[3].Y, dstPt[0].X, dstPt[0].Y);

? ? ? ? ? ? pictureBox1.Image = bmp;

? ? ? ? }

? ? ? ? /// <summary>
? ? ? ? /// 剪裁圖片
? ? ? ? /// </summary>
? ? ? ? /// <param name="src">原圖片</param>
? ? ? ? /// <param name="left">左坐標(biāo)</param>
? ? ? ? /// <param name="top">頂部坐標(biāo)</param>
? ? ? ? /// <param name="right">右坐標(biāo)</param>
? ? ? ? /// <param name="bottom">底部坐標(biāo)</param>
? ? ? ? /// <returns>剪裁后的圖片</returns>
? ? ? ? public Image CutImage(Image src, int left, int top, int right, int bottom)
? ? ? ? {
? ? ? ? ? ? Bitmap srcBitmap = new Bitmap(src);
? ? ? ? ? ? int width = right - left;
? ? ? ? ? ? int height = bottom - top;
? ? ? ? ? ? Bitmap destBitmap = new Bitmap(width, height);
? ? ? ? ? ? using (Graphics g = Graphics.FromImage(destBitmap))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? g.Clear(Color.Transparent);
? ? ? ? ? ? ? ? //設(shè)置畫布的描繪質(zhì)量 ? ? ? ??
? ? ? ? ? ? ? ? g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
? ? ? ? ? ? ? ? g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
? ? ? ? ? ? ? ? g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
? ? ? ? ? ? ? ? g.DrawImage(srcBitmap, new Rectangle(0, 0, width, height), left, top, width, height, GraphicsUnit.Pixel);
? ? ? ? ? ? }
? ? ? ? ? ? return destBitmap;
? ? ? ? }
? ? }
}?

下載

源碼下載

?文章來源地址http://www.zghlxwxcb.cn/news/detail-614828.html

到了這里,關(guān)于C# OpenCvSharp 圖像校正的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • C#使用OpenCv(OpenCVSharp)圖像處理實例:亮度、對比度、灰度

    本文實例演示C#語言中如何使用OpenCv(OpenCVSharp)對圖像進(jìn)行亮度、對比度、灰度處理。 目錄 亮度和對比度原理 灰度 實例 圖像亮度通俗理解便是圖像的明暗程度,數(shù)字圖像?f(x,y) = i(x,y) r(x, y)?,如果灰度值在[0,255]之間,則?f?值越接近0亮度越低,f?值越接近255亮度越

    2024年02月13日
    瀏覽(27)
  • C# OpenCvSharp 輪廓檢測

    C# OpenCvSharp 輪廓檢測

    目錄 效果 代碼 下載? using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using OpenCvSharp; using OpenCvSharp.Extensions; namespace OpenCvSharp_輪廓檢測 { ? ? public partial class Form1 : Form ? ? { ? ? ? ?

    2024年04月15日
    瀏覽(20)
  • C#圖像處理-OpenCVSharp教程:OpenCVSharp與EmguCV的比較與介紹

    C#圖像處理-OpenCVSharp教程:OpenCVSharp與EmguCV的比較與介紹 圖像處理在計算機(jī)視覺和計算機(jī)圖形學(xué)等領(lǐng)域發(fā)揮著至關(guān)重要的作用。本教程將介紹在C#中使用OpenCVSharp和EmguCV這兩個流行的圖像處理庫,它們提供了豐富的功能和強(qiáng)大的性能。 一、OpenCVSharp介紹與特點 OpenCVSharp是OpenCV的

    2024年02月21日
    瀏覽(25)
  • c# OpenCvSharp 檢測(斑點檢測、邊緣檢測、輪廓檢測)(五)

    c# OpenCvSharp 檢測(斑點檢測、邊緣檢測、輪廓檢測)(五)

    在C#中使用OpenCV進(jìn)行圖像處理時,可以使用不同的算法和函數(shù)來實現(xiàn)斑點檢測、邊緣檢測和輪廓檢測。 斑點檢測 邊緣檢測 輪廓檢測 斑點檢測是指在圖像中找到明亮或暗的小區(qū)域(通常表示為斑點),并標(biāo)記它們的位置。可以使用OpenCV中的函數(shù)SimpleBlobDetector來實現(xiàn)斑點檢測。

    2024年02月04日
    瀏覽(12)
  • C#使用OpenCv(OpenCVSharp)教程詳解

    本篇講解C#中如何使用OpenCV(OpenCVSharp) 目錄 前言 OpenCVSharp安裝 OpenCVSharp使用 實例讀取圖像并顯示

    2024年02月13日
    瀏覽(17)
  • c# OpenCvSharp讀取、顯示和寫入圖像(二)

    c# OpenCvSharp讀取、顯示和寫入圖像(二)

    ????????讀取、顯示和寫入圖像是圖像處理和計算機(jī)視覺的基礎(chǔ)。即使在裁剪、調(diào)整大小、旋轉(zhuǎn)或應(yīng)用不同的濾鏡來處理圖像時,您也需要先讀取圖像。因此,掌握這些基本操作非常重要。 imread()讀取圖像 imshow()在窗口中顯示圖像 imwrite()將圖像保存到文件目錄里 我們將使

    2024年02月02日
    瀏覽(23)
  • c# OpenCvSharp圖像裁剪、調(diào)整大小、旋轉(zhuǎn)、透視(三)

    圖像裁剪、調(diào)整大小、旋轉(zhuǎn)、透視圖像處理基本操作。 croppedImage?圖像裁剪 Cv2.Resize() 調(diào)整圖像大小 圖像旋轉(zhuǎn) Cv2.Rotate()旋轉(zhuǎn) Cv2.Flip()翻轉(zhuǎn) Cv2.WarpAffine()任意角度旋轉(zhuǎn) Cv2.GetAffineTransform()透視 Rect rect = new Rect(x, y, width, height); // x, y 為起始坐標(biāo),width, height 為裁剪寬高 參數(shù) 說明

    2024年02月04日
    瀏覽(12)
  • C#圖像處理-使用OpenCVSharp讀取或修改圖像像素值

    圖像處理是計算機(jī)視覺領(lǐng)域的重要應(yīng)用之一,而OpenCV是一個強(qiáng)大且廣泛使用的開源計算機(jī)視覺庫。在C#中,我們可以通過OpenCVSharp庫來實現(xiàn)圖像處理的各種功能,包括讀取和修改圖像像素值。本文將介紹如何使用OpenCVSharp來讀取和修改圖像像素值,并提供相應(yīng)的源代碼。 首先,

    2024年04月28日
    瀏覽(128)
  • C# OpenCvSharp Yolov8 Detect 目標(biāo)檢測

    C# OpenCvSharp Yolov8 Detect 目標(biāo)檢測

    目錄 效果 模型信息 項目 代碼 下載? Model Properties ------------------------- date:2023-09-05T13:17:15.396588 description:Ultralytics YOLOv8n model trained on coco.yaml author:Ultralytics task:detect license:AGPL-3.0 https://ultralytics.com/license version:8.0.170 stride:32 batch:1 imgsz:[640, 640] names:{0: \\\'person\\\', 1: \\\'b

    2024年02月02日
    瀏覽(18)
  • C# OpenCvSharp Yolov8 Cls 圖像分類

    C# OpenCvSharp Yolov8 Cls 圖像分類

    目錄 效果 項目 模型信息 代碼 下載? Model Properties ------------------------- date:2023-09-07T17:11:37.011156 description:Ultralytics YOLOv8n-cls model trained on ../datasets/imagenet author:Ultralytics task:classify license:AGPL-3.0 https://ultralytics.com/license version:8.0.172 stride:1 batch:1 imgsz:[640, 640] names:{0:

    2024年02月07日
    瀏覽(23)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包