OpenCV學習(7.16)

寫了個實現攝像頭上畫線并輸出角度的東西……雖然很簡單,但腦殘的我還是debug了很長時間。

 1 // 圓和直線.cpp : 定義控制臺應用程序的入口點。
 2 //
 3 
 4 #include "stdafx.h"
 5 
 6 using namespace std;
 7 using namespace cv;
 8 
 9 void onMouse(int event, int x, int y, int flags, void* param);
10 
11 Mat frame;
12 Point p1, p2;
13 bool flag = true;
14 
15 int main()
16 {
17   VideoCapture capture;
18   capture.open(0);
19 
20   namedWindow("test", WINDOW_AUTOSIZE);
21   setMouseCallback("test", onMouse);
22 
23   while (1)
24   {
25     capture >> frame;
26     line(frame, p1, p2, 0, 1, 8, 0);
27     imshow("test", frame);
28     waitKey(33);
29   }
30   return 0;
31 }
32 
33 void onMouse(int event, int x, int y, int flags, void* param)
34 {
35 
36   switch (event)
37   {
38   case CV_EVENT_MOUSEMOVE:
39     if (flag == true) { p1.x = x; p1.y = y - 50; p2.x = x; p2.y = y + 50; }
40     else { p2.x = x; p2.y = y; }
41     break;
42   case CV_EVENT_LBUTTONDOWN:
43     flag = false; p1.x = x; p1.y = y; break;
44   case CV_EVENT_LBUTTONUP:
45     int X = x - p1.x, Y = p1.y - y;
46     double r = sqrt(fabs(X*X) + fabs(Y*Y));
47     //cout <<X << ' ' << Y << endl;
48     double e = 180.0 / 3.1415926, line_degree;
49     if (X > 0)
50     {
51       if (Y >= 0) line_degree = asin(Y / r)*e;
52       else line_degree = asin(Y / r)*e;
53     }
54     else if (X < 0)
55     {
56     if (Y >= 0) line_degree = 180-asin(Y / r)*e;
57     else line_degree = -asin(Y / r)*e - 180.0;
58     }
59   cout << "角度=" << line_degree << endl;
60   flag = true;
61   break;
62   }
63   return;
64 }

?

鼠標標識掩膜:
實現比較簡單……就是新建一個圖片,在那個圖片中畫圓,需要的時候用addweight函數把兩張照片融合到一起,但是因為我掌握了直接畫圓的技術所以就不需要這個了。

串口通信程序:
這個地方似乎要用到C#。在VS中寫C#的話就需要在:
“屬性管理器”->“配置屬性”->“常規”->“公共語言運行時支持”中選擇“公共語言運行時支持”。
但打開公共支持之后還是不行……
//
好的,要打開“屬性管理器”,在“配置”處選擇“所有配置”。


下面的代碼是從Microsoft的官網上拿來的例程,但注釋是我自己寫得。主要原因是學長從網上找的是C#的例程,而我找的是C++程序。
寫了一堆注釋……定義了一些初始值。

// 串口通信C++.cpp : 定義控制臺應用程序的入口點。
//

#include "stdafx.h"
#using <System.dll>//程序集using namespace System;
using namespace System::IO::Ports;
using namespace System::Threading;public ref class PortChat
{
private:
static bool _continue;
static SerialPort^ _serialPort;public:
static void Main()
{
String^ name;//“^”表示聲明一個托管類型的字符串指針。特點是內存由GC管理,占用的內存會被自動釋放
String^ message;
StringComparer^ stringComparer = StringComparer::OrdinalIgnoreCase;//表示一種字符串比較操作,該操作使用特定的大小寫以及基于區域性的比較規則或序號比較規則。
Thread^ readThread = gcnew Thread(gcnew ThreadStart(PortChat::Read));//新建線程// Create a new SerialPort object with default settings.
_serialPort = gcnew SerialPort();// 設置各種數據
_serialPort->PortName = SetPortName("COM3");//設置通信湍口(這里可以用COM端口)
_serialPort->BaudRate = SetPortBaudRate(9600);//設置串行波特率
_serialPort->Parity = SetPortParity(Parity::None);//設置奇偶校驗檢查協議
_serialPort->DataBits = SetPortDataBits(8);//設置每個字節的標準數據位長度
_serialPort->StopBits = SetPortStopBits(StopBits::One);//設置標準停止位數
//_serialPort->Handshake = SetPortHandshake(_serialPort->Handshake);//設置串行端口數據傳輸的握手協議// Set the read/write timeouts
//_serialPort->ReadTimeout = 500;//設置讀取操作未完成時發生超時之前的毫秒數
//_serialPort->WriteTimeout = 500;//設置寫入操作未完成時發生超時之前的毫秒數

_serialPort->Open();//打開端口
_continue = true;//是否繼續傳輸
//readThread->Start();//Console::Write("Name: ");//console即控制臺。似乎這句話的意思是輸出到控制臺
name = Console::ReadLine();//一直讀取到輸入緩沖區中的 NewLine 值。
//SerialPort::NewLine 屬性 
//獲取或設置用于解釋 ReadLine 和 WriteLine 方法調用結束的值。
//表示行尾的值,默認值為換行符。
//Console::WriteLine("Type QUIT to exit");while (_continue)
{
message = Console::ReadLine();if (stringComparer->Equals("quit", message))//如果是quit就退出(話說前面明明說的是大寫……)
{
_continue = false;
}
else
{
_serialPort->WriteLine(//將指定的字符串和NewLine值寫入輸出緩沖區
String::Format("<{0}>: {1}", name, message));
//String::Format用來生成一個靜態字符串
//此處生成的應該是:“<name>:message”
}
}readThread->Join();//令線程處于阻塞狀態
_serialPort->Close();//關閉串口
}static void Read()
{
while (_continue)
{
try
{
String^ message = _serialPort->ReadLine();
Console::WriteLine(message);
}
catch (TimeoutException ^) {}//超時的話就算是發生了錯誤……是這個意思吧
}
}static String^ SetPortName(String^ defaultPortName)
{
String^ portName;Console::WriteLine("Available Ports:");
for each (String^ s in SerialPort::GetPortNames())
{
Console::WriteLine(" {0}", s);
}Console::Write("Enter COM port value (Default: {0}): ", defaultPortName);
portName = Console::ReadLine();if (portName == "")
{
portName = defaultPortName;
}
return portName;
}static Int32 SetPortBaudRate(Int32 defaultPortBaudRate)
{
String^ baudRate;Console::Write("Baud Rate(default:{0}): ", defaultPortBaudRate);
baudRate = Console::ReadLine();if (baudRate == "")
{
baudRate = defaultPortBaudRate.ToString();
}return Int32::Parse(baudRate);
}static Parity SetPortParity(Parity defaultPortParity)
{
String^ parity;Console::WriteLine("Available Parity options:");
for each (String^ s in Enum::GetNames(Parity::typeid))
{
Console::WriteLine(" {0}", s);
}Console::Write("Enter Parity value (Default: {0}):", defaultPortParity.ToString());
parity = Console::ReadLine();if (parity == "")
{
parity = defaultPortParity.ToString();
}return (Parity)Enum::Parse(Parity::typeid, parity);
}static Int32 SetPortDataBits(Int32 defaultPortDataBits)
{
String^ dataBits;Console::Write("Enter DataBits value (Default: {0}): ", defaultPortDataBits);
dataBits = Console::ReadLine();if (dataBits == "")
{
dataBits = defaultPortDataBits.ToString();
}return Int32::Parse(dataBits);
}static StopBits SetPortStopBits(StopBits defaultPortStopBits)
{
String^ stopBits;Console::WriteLine("Available Stop Bits options:");
for each (String^ s in Enum::GetNames(StopBits::typeid))//for each可以理解為for語句對特定數據結構的遍歷過程的優化……
{
Console::WriteLine(" {0}", s);
}Console::Write("Enter StopBits value (None is not supported and \n" +
"raises an ArgumentOutOfRangeException. \n (Default: {0}):", defaultPortStopBits.ToString());
stopBits = Console::ReadLine();if (stopBits == "")
{
stopBits = defaultPortStopBits.ToString();
}return (StopBits)Enum::Parse(StopBits::typeid, stopBits);
}static Handshake SetPortHandshake(Handshake defaultPortHandshake)
{
String^ handshake;Console::WriteLine("Available Handshake options:");
for each (String^ s in Enum::GetNames(Handshake::typeid))
{
Console::WriteLine(" {0}", s);
}Console::Write("Enter Handshake value (Default: {0}):", defaultPortHandshake.ToString());
handshake = Console::ReadLine();if (handshake == "")
{
handshake = defaultPortHandshake.ToString();
}return (Handshake)Enum::Parse(Handshake::typeid, handshake);}
//這個協議感覺可能和加密有關?
};int main()
{
PortChat::Main();
}

?

?

?

?

?

轉載于:https://www.cnblogs.com/Shymuel/p/9327516.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/390096.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390096.shtml
英文地址,請注明出處:http://en.pswp.cn/news/390096.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

學習vue.js的自我梳理筆記

基本語法格式&#xff1a; <script> new Vue({ el: #app, data: { url: http://www.runoob.com } }) </script> 指令 【指令是帶有 v- 前綴的特殊屬性。】 判斷 <p v-if"seen">現在你看到我了</p> 參數 <a v-bind:href"url"&…

722. 刪除注釋

722. 刪除注釋 給一個 C 程序&#xff0c;刪除程序中的注釋。這個程序source是一個數組&#xff0c;其中source[i]表示第i行源碼。 這表示每行源碼由\n分隔。 在 C 中有兩種注釋風格&#xff0c;行內注釋和塊注釋。 字符串// 表示行注釋&#xff0c;表示//和其右側的其余字符…

如何創建一個自記錄的Makefile

My new favorite way to completely underuse a Makefile? Creating personalized, per-project repository workflow command aliases that you can check in.我最喜歡的完全沒用Makefile的方法&#xff1f; 創建個性化的按項目存儲庫工作流命令別名&#xff0c;您可以檢入。…

【BZOJ3262】陌上花開

CDQ分治模板 注意三元組完全相等的情況 1 #include<bits/stdc.h>2 using namespace std;3 const int N100010,K200010;4 int n,k,cnt[N],ans[N];5 struct Node{6 int a,b,c,id;7 bool operator<(const Node& k)const{8 if(bk.b&&ck.c) re…

Spring+jpaNo transactional EntityManager available

2019獨角獸企業重金招聘Python工程師標準>>> TransactionRequiredException: No transactional EntityManager availableEntityManager執行以下方法(refresh, persist, flush, joinTransaction, remove, merge) 都需要需要事務if (transactionRequiringMethods.cont…

python項目構建_通過構建4個項目來學習Python網絡

python項目構建The Python programming language is very capable when it comes to networking. Weve released a crash course on the freeCodeCamp.org YouTube channel that will help you learn the basics of networking in Python.當涉及到網絡時&#xff0c;Python編程…

164. 最大間距

164. 最大間距 給定一個無序的數組&#xff0c;找出數組在排序之后&#xff0c;相鄰元素之間最大的差值。 如果數組元素個數小于 2&#xff0c;則返回 0。 示例 1: 輸入: [3,6,9,1] 輸出: 3 解釋: 排序后的數組是 [1,3,6,9], 其中相鄰元素 (3,6) 和 (6,9) 之間都存在最大差…

10.32/10.33 rsync通過服務同步 10.34 linux系統日志 screen工具

通過后臺服務的方式在遠程主機上建立一個rsync的服務器&#xff0c;在服務器上配置好rsync的各種應用&#xff0c;然后將本機作為rsync的一個客戶端連接遠程的rsync服務器。在128主機上建立并配置rsync的配置文件/etc/rsyncd.conf,把你的rsyncd.conf編輯成以下內容&#xff1a;…

01_Struts2概述及環境搭建

1.Struts2概述&#xff1a;Struts2是一個用來開發MVC應用程序的框架。Struts2提供了web應用程序開發過程中一些常見問題的解決方案;對用戶輸入的數據進行合法性驗證統一的布局可擴展性國際化和本地化支持Ajax表單的重復提交文件的上傳和下載... ...2.Struts2相對于Struts1的優勢…

315. 計算右側小于當前元素的個數

315. 計算右側小于當前元素的個數 給定一個整數數組 nums&#xff0c;按要求返回一個新數組 counts。數組 counts 有該性質&#xff1a; counts[i] 的值是 nums[i] 右側小于 nums[i] 的元素的數量。 示例&#xff1a; 輸入&#xff1a;nums [5,2,6,1] 輸出&#xff1a;[2,1…

IOS上傳文件給java服務器,返回報錯unacceptable context-type:text/plain

IOS上傳文件給java服務器&#xff0c;返回報錯unacceptable context-type&#xff1a;text/plain response返回類型不對 RequestMapping(value "uploadMultiFiles", method RequestMethod.POST, produces"application/json;charsetUTF-8") 使用produces指…

Python爬蟲框架Scrapy學習筆記原創

字號scrapy [TOC] 開始 scrapy安裝 首先手動安裝windows版本的Twisted https://www.lfd.uci.edu/~gohlke/pythonlibs/#twisted pip install Twisted-18.4.0-cp36-cp36m-win_amd64.whl 安裝scrapy pip install -i https://pypi.douban.com/simple/ scrapy windows系統額外需要…

600. 不含連續1的非負整數

600. 不含連續1的非負整數 給定一個正整數 n&#xff0c;找出小于或等于 n 的非負整數中&#xff0c;其二進制表示不包含 連續的1 的個數。 示例 1:輸入: 5 輸出: 5 解釋: 下面是帶有相應二進制表示的非負整數< 5&#xff1a; 0 : 0 1 : 1 2 : 10 3 : 11 4 : 100 5 : 101…

高可用性、負載均衡的mysql集群解決方案

2019獨角獸企業重金招聘Python工程師標準>>> 一、為什么需要mysql集群&#xff1f; 一個龐大的分布式系統的性能瓶頸中&#xff0c;最脆弱的就是連接。連接有兩個&#xff0c;一個是客戶端與后端的連接&#xff0c;另一個是后端與數據庫的連接。簡單如圖下兩個藍色框…

Django的model查詢操作 與 查詢性能優化

Django的model查詢操作 與 查詢性能優化 1 如何 在做ORM查詢時 查看SQl的執行情況 (1) 最底層的 django.db.connection 在 django shell 中使用 python manage.py shell>>> from django.db import connection >>> Books.objects.all() >>> connect…

887. 雞蛋掉落

887. 雞蛋掉落 給你 k 枚相同的雞蛋&#xff0c;并可以使用一棟從第 1 層到第 n 層共有 n 層樓的建筑。 已知存在樓層 f &#xff0c;滿足 0 < f < n &#xff0c;任何從 高于 f 的樓層落下的雞蛋都會碎&#xff0c;從 f 樓層或比它低的樓層落下的雞蛋都不會破。 每次…

678. 有效的括號字符串

678. 有效的括號字符串 給定一個只包含三種字符的字符串&#xff1a;&#xff08; &#xff0c;&#xff09; 和 *&#xff0c;寫一個函數來檢驗這個字符串是否為有效字符串。有效字符串具有如下規則&#xff1a; 任何左括號 ( 必須有相應的右括號 )。任何右括號 ) 必須有相應…

Faster R-CNN代碼例子

主要參考文章&#xff1a;1&#xff0c;從編程實現角度學習Faster R-CNN&#xff08;附極簡實現&#xff09; 經常是做到一半發現收斂情況不理想&#xff0c;然后又回去看看這篇文章的細節。 另外兩篇&#xff1a; 2&#xff0c;Faster R-CNN學習總結 這個主要是解釋了18,…

剝開比原看代碼09:通過dashboard創建密鑰時,前端的數據是如何傳到后端的?

2019獨角獸企業重金招聘Python工程師標準>>> 作者&#xff1a;freewind 比原項目倉庫&#xff1a; Github地址&#xff1a;https://github.com/Bytom/bytom Gitee地址&#xff1a;https://gitee.com/BytomBlockchain/bytom 在前面一篇文章&#xff0c;我們粗略的研究…

面試題 17.24. 最大子矩陣

面試題 17.24. 最大子矩陣 給定一個正整數、負整數和 0 組成的 N M 矩陣&#xff0c;編寫代碼找出元素總和最大的子矩陣。 返回一個數組 [r1, c1, r2, c2]&#xff0c;其中 r1, c1 分別代表子矩陣左上角的行號和列號&#xff0c;r2, c2 分別代表右下角的行號和列號。若有多個…