" />

欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

OpenCV實現(xiàn)簡單套索工具

 更新時間:2022年01月19日 11:39:16   作者:蓬 蒿 人  
這篇文章主要為大家詳細介紹了OpenCV實現(xiàn)簡單套索工具,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

Photoshop中的套索工具通過鼠標多次點擊可以選中一個任意多邊形的區(qū)域,然后單獨對這塊區(qū)域進行編輯,下面就使用OpenCV實現(xiàn)一個簡單的功能,模擬Photoshop中的套索工具。

這里的套索工具通過鼠標左鍵在圖片上多次點擊創(chuàng)建任意多個點,右鍵點擊后將這些點連成封閉的多邊形,形成一塊待編輯的區(qū)域,鍵盤方向鍵控制該區(qū)域的移動,從而將該區(qū)域內(nèi)的圖像復制到原圖像的其他地方。

首先定義下列全局變量

const char* winName = "TaoSuoTool";//窗口名稱
cv::Mat resultImg;//最終在OpenCV窗口上顯示的圖像
cv::Mat foregroundImg;//編輯前的圖像
cv::Mat areaMask;//蒙版,多邊形區(qū)域?qū)嶋H繪制在該蒙版上
cv::Point maskLocation;//蒙版位置,通過方向鍵移動后蒙版位置隨之變化
std::vector<cv::Point> drawingPoints;//區(qū)域完成前正在點擊的所有點
std::vector<cv::Point> areaPoints;//區(qū)域完成后其多邊形頂點

main函數(shù)

int main(int argc, char **arv)
{
? ? foregroundImg = cv::imread("test.jpg");
? ? foregroundImg.copyTo(resultImg);
? ? areaMask = cv::Mat::zeros(foregroundImg.size(), CV_8U);
? ? cv::imshow(winName, resultImg);
?
? ? maskLocation.x = maskLocation.y = 0;
? ? cv::setMouseCallback(winName, OnMouseEvent);
? ? int key = cv::waitKeyEx(0);
? ? while (key != VK_ESCAPE)
? ? {
? ? ? ? key = cv::waitKeyEx(0);
? ? }
? ? return 0;
}

在鼠標回調(diào)函數(shù)OnMouseEvent中處理三個消息:鼠標左鍵按下,鼠標右鍵按下和鼠標移動

void OnMouseEvent(int event, int x, int y, int flags, void* userdata)
{
? ? if (event == cv::EVENT_LBUTTONDOWN)
? ? {
? ? ? ? OnLeftMouseButtonDown(x,y);
? ? }
? ? else if (event == cv::EVENT_RBUTTONDOWN)
? ? {
? ? ? ? OnRightMouseButtonDown(x,y);
? ? }
? ? if (event == cv::EVENT_MOUSEMOVE)
? ? {
? ? ? ? OnMouseMove(x,y);
? ? }
}

在編寫鼠標事件前先定義一個函數(shù)

void OnCompleteArea(bool bDrawOutline);

它表示完成當前區(qū)域的編輯,包括右鍵點擊完成封閉多邊形、移動區(qū)域以及合成最終圖片。參數(shù)bDrawOutline表示繪制區(qū)域多邊形的外輪廓,右鍵點擊完成封閉多邊形和移動區(qū)域過程中都要顯示輪廓(bDrawOutline=true),合成最終圖片后就不需要顯示輪廓了(bDrawOutline=false)。
鼠標左鍵按下事件:先判斷是否有前一個區(qū)域存在,存在則先完成前一個區(qū)域并且不顯示區(qū)域輪廓,然后開始繪制新的區(qū)域多邊形的點,點與點之間用藍色線連接,點位置處繪制一個4X4的紅色矩形。

void OnLeftMouseButtonDown(int x,int y)
{
? ? if (drawingPoints.empty() && areaPoints.size() > 0)
? ? {
? ? ? ? OnCompleteArea(false);
? ? }
? ? drawingPoints.push_back(cv::Point(x, y));
? ? cv::rectangle(resultImg, cv::Rect(x - 2, y - 2, 4, 4), CV_RGB(255, 0, 0), -1);
? ? if (drawingPoints.size() >= 2)
? ? {
? ? ? ? cv::line(resultImg, drawingPoints[drawingPoints.size() - 2], cv::Point(x, y), CV_RGB(0, 0, 255), 1);
? ? }
? ? cv::imshow(winName, resultImg);
}

鼠標移動事件:判斷drawingPoints是否為空,如果已經(jīng)存在點則繪制線和點,并且還要繪制一根連接到鼠標當前位置的線。

void OnMouseMove(int x,int y)
{
? ? if (drawingPoints.size() > 0)
? ? {
? ? ? ? foregroundImg.copyTo(resultImg);
? ? ? ? for (int i = 0; i < drawingPoints.size() - 1; i++)
? ? ? ? {
? ? ? ? ? ? cv::rectangle(resultImg, cv::Rect(drawingPoints[i].x - 2, drawingPoints[i].y - 2, 4, 4), CV_RGB(255, 0, 0), -1);
? ? ? ? ? ? cv::line(resultImg, drawingPoints[i], drawingPoints[i + 1], CV_RGB(0, 0, 255), 1);
? ? ? ? }
? ? ? ? cv::rectangle(resultImg, cv::Rect(drawingPoints[drawingPoints.size() - 1].x - 2, drawingPoints[drawingPoints.size() - 1].y - 2, 4, 4), CV_RGB(255, 0, 0), -1);
? ? ? ? cv::line(resultImg, drawingPoints[drawingPoints.size() - 1], cv::Point(x, y), CV_RGB(0, 0, 255), 1);
? ? ? ? cv::imshow(winName, resultImg);
? ? }
}

鼠標右鍵按下事件:如果點個數(shù)少于2,不能形成有效區(qū)域則不做處理(不考慮多個點共線),否則就在蒙版Area上繪制一個多邊形區(qū)域,然后調(diào)用OnCompleteArea完成區(qū)域編輯,這里需要畫多邊形輪廓,參數(shù)傳入true。

void OnRightMouseButtonDown(int x,int y)
{
? ? if (drawingPoints.size() >= 3)
? ? {
? ? ? ? areaPoints = drawingPoints;
? ? ? ? std::vector<std::vector<cv::Point>> polys;
? ? ? ? polys.push_back(areaPoints);
? ? ? ? cv::fillPoly(areaMask, polys, cv::Scalar::all(255));
? ? ? ? OnCompleteArea(true);
? ? }
? ? else
? ? {
? ? ? ? foregroundImg.copyTo(resultImg);
? ? }
? ? drawingPoints.clear();
? ? cv::imshow(winName, resultImg);
}

下面是OnCompleteArea函數(shù)的實現(xiàn),其中MergeImages函數(shù)通過蒙版以及蒙版的位置合成最終的圖像,蒙版中區(qū)域內(nèi)的像素值大于0,其他像素值都為0,默認圖像是三通道(destImg.at<cv::Vec3b>)

void MergeImages(cv::Mat& destImg, const cv::Mat& srcImg, const cv::Mat& maskImg, int x, int y)
{
? ? int top = y > 0 ? y : 0;
? ? int left = x > 0 ? x : 0;
? ? int right = destImg.cols > x + srcImg.cols ? x + srcImg.cols : destImg.cols;
? ? int bottom = destImg.rows > y + srcImg.rows ? y + srcImg.rows : destImg.rows;
? ? for (int i = top; i < bottom; i++)
? ? {
? ? ? ? for (int j = left; j < right; j++)
? ? ? ? {
? ? ? ? ? ? int destIndex = i * destImg.cols + j;
? ? ? ? ? ? int srcIndex = (i - top)*srcImg.cols + j - left;
? ? ? ? ? ? int channel = destImg.channels();
? ? ? ? ? ? if (maskImg.at<uchar>(i - y, j - x) > 0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? destImg.at<cv::Vec3b>(i, j)[0] = srcImg.at<cv::Vec3b>(i - y, j - x)[0];
? ? ? ? ? ? ? ? destImg.at<cv::Vec3b>(i, j)[1] = srcImg.at<cv::Vec3b>(i - y, j - x)[1];
? ? ? ? ? ? ? ? destImg.at<cv::Vec3b>(i, j)[2] = srcImg.at<cv::Vec3b>(i - y, j - x)[2];
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
void OnCompleteArea(bool bDrawOutline)
{
? ? foregroundImg.copyTo(resultImg);
? ? MergeImages(resultImg, foregroundImg, areaMask, maskLocation.x, maskLocation.y);
? ? if (bDrawOutline)
? ? {
? ? ? ? if (areaPoints.size() >= 3)
? ? ? ? {
? ? ? ? ? ? std::vector<std::vector<cv::Point>> polys;
? ? ? ? ? ? polys.push_back(areaPoints);
? ? ? ? ? ? cv::polylines(resultImg, polys, true, cv::Scalar::all(255));
? ? ? ? }
? ? }
? ? else
? ? {
? ? ? ? resultImg.copyTo(foregroundImg);
? ? ? ? areaPoints.clear();
? ? ? ? memset(areaMask.data, 0, areaMask.rows*areaMask.cols*areaMask.elemSize());
? ? ? ? maskLocation.x = maskLocation.y = 0;
? ? }
}

繪制區(qū)域之后就可以通過方向按鍵控制區(qū)域圖像的移動了(也可以實現(xiàn)為鼠標左鍵按下拖動來移動區(qū)域),移動主要是更新maskLocation和areaPoints的坐標值,然后調(diào)用OnCompleteArea(true),依然顯示區(qū)域的輪廓。

void OnDirectionKeyDown(short keyCode)
{
? ? int x = keyCode == VK_LEFT ? -2 : (keyCode == VK_RIGHT ? 2 : 0);
? ? int y = keyCode == VK_UP ? -2 : (keyCode == VK_DOWN ? 2 : 0);
? ? maskLocation.x += x;
? ? maskLocation.y += y;
? ? for (int i = 0; i < areaPoints.size(); i++)
? ? {
? ? ? ? areaPoints[i].x += x;
? ? ? ? areaPoints[i].y += y;
? ? }
? ? OnCompleteArea(true);
? ? cv::imshow(winName, resultImg);
}

將上面函數(shù)在主函數(shù)的按鍵循環(huán)中調(diào)用,方向按鍵通過key的高16位判斷,在Windows下可以使用虛擬鍵碼宏表示。 同時為了能看到最終合成的圖片加入Enter按鍵消息處理,將圖像合成并去掉輪廓。

int key = cv::waitKeyEx(0);
short lowKey = key;
short highKey = key >> 16;
while (key != VK_ESCAPE)
{
? ? if (key == VK_RETURN)//Enter
? ? {
? ? ? ? OnCompleteArea(false);
? ? ? ? cv::imshow(winName, resultImg);
? ? }
? ? else if (lowKey == 0 && (highKey == VK_UP || highKey == VK_DOWN || highKey == VK_LEFT || highKey == VK_RIGHT))
? ? {
? ? ? ? OnDirectionKeyDown(highKey);
? ? }
? ? key = cv::waitKeyEx(0);
? ? lowKey = key;
? ? highKey = key >> 16;
}

這樣一個簡單的套索工具功能就做好了(上面的代碼都是簡化處理,還有很多可以優(yōu)化的地方,從而使編輯更加流暢)

完整代碼

#include<iostream>
#include"opencv2/opencv.hpp"
#include<windows.h>
const char* winName = "TaoSuoTool";
cv::Point maskLocation;
cv::Mat resultImg;
cv::Mat foregroundImg;
cv::Mat areaMask;
std::vector<cv::Point> drawingPoints;
std::vector<cv::Point> areaPoints;
void MergeImages(cv::Mat& destImg, const cv::Mat& srcImg, const cv::Mat& maskImg, int x, int y)
{
? ? int top = y > 0 ? y : 0;
? ? int left = x > 0 ? x : 0;
? ? int right = destImg.cols > x + srcImg.cols ? x + srcImg.cols : destImg.cols;
? ? int bottom = destImg.rows > y + srcImg.rows ? y + srcImg.rows : destImg.rows;
? ? for (int i = top; i < bottom; i++)
? ? {
? ? ? ? for (int j = left; j < right; j++)
? ? ? ? {
? ? ? ? ? ? int destIndex = i * destImg.cols + j;
? ? ? ? ? ? int srcIndex = (i - top)*srcImg.cols + j - left;
? ? ? ? ? ? int channel = destImg.channels();
? ? ? ? ? ? if (maskImg.at<uchar>(i - y, j - x) > 0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? destImg.at<cv::Vec3b>(i, j)[0] = srcImg.at<cv::Vec3b>(i - y, j - x)[0];
? ? ? ? ? ? ? ? destImg.at<cv::Vec3b>(i, j)[1] = srcImg.at<cv::Vec3b>(i - y, j - x)[1];
? ? ? ? ? ? ? ? destImg.at<cv::Vec3b>(i, j)[2] = srcImg.at<cv::Vec3b>(i - y, j - x)[2];
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
?
void OnCompleteArea(bool bDrawOutline)
{
? ? foregroundImg.copyTo(resultImg);
? ? MergeImages(resultImg, foregroundImg, areaMask, maskLocation.x, maskLocation.y);
? ? if (bDrawOutline)
? ? {
? ? ? ? if (areaPoints.size() >= 3)
? ? ? ? {
? ? ? ? ? ? std::vector<std::vector<cv::Point>> polys;
? ? ? ? ? ? polys.push_back(areaPoints);
? ? ? ? ? ? cv::polylines(resultImg, polys, true, cv::Scalar::all(255));
? ? ? ? }
? ? }
? ? else
? ? {
? ? ? ? resultImg.copyTo(foregroundImg);
? ? ? ? areaPoints.clear();
? ? ? ? memset(areaMask.data, 0, areaMask.rows*areaMask.cols*areaMask.elemSize());
? ? ? ? maskLocation.x = maskLocation.y = 0;
? ? }
}
void OnLeftMouseButtonDown(int x,int y)
{
? ? if (drawingPoints.empty() && areaPoints.size() > 0)
? ? {
? ? ? ? OnCompleteArea(false);
? ? }
? ? drawingPoints.push_back(cv::Point(x, y));
? ? cv::rectangle(resultImg, cv::Rect(x - 2, y - 2, 4, 4), CV_RGB(255, 0, 0), -1);
? ? if (drawingPoints.size() >= 2)
? ? {
? ? ? ? cv::line(resultImg, drawingPoints[drawingPoints.size() - 2], cv::Point(x, y), CV_RGB(0, 0, 255), 1);
? ? }
? ? cv::imshow(winName, resultImg);
}
void OnRightMouseButtonDown(int x,int y)
{
? ? if (drawingPoints.size() >= 3)
? ? {
? ? ? ? areaPoints = drawingPoints;
? ? ? ? std::vector<std::vector<cv::Point>> polys;
? ? ? ? polys.push_back(areaPoints);
? ? ? ? cv::fillPoly(areaMask, polys, cv::Scalar::all(255));
? ? ? ? OnCompleteArea(true);
? ? }
? ? else
? ? {
? ? ? ? foregroundImg.copyTo(resultImg);
? ? }
? ? drawingPoints.clear();
? ? cv::imshow(winName, resultImg);
}
void OnMouseMove(int x,int y)
{
? ? if (drawingPoints.size() > 0)
? ? {
? ? ? ? foregroundImg.copyTo(resultImg);
? ? ? ? for (int i = 0; i < drawingPoints.size() - 1; i++)
? ? ? ? {
? ? ? ? ? ? cv::rectangle(resultImg, cv::Rect(drawingPoints[i].x - 2, drawingPoints[i].y - 2, 4, 4), CV_RGB(255, 0, 0), -1);
? ? ? ? ? ? cv::line(resultImg, drawingPoints[i], drawingPoints[i + 1], CV_RGB(0, 0, 255), 1);
? ? ? ? }
? ? ? ? cv::rectangle(resultImg, cv::Rect(drawingPoints[drawingPoints.size() - 1].x - 2, drawingPoints[drawingPoints.size() - 1].y - 2, 4, 4), CV_RGB(255, 0, 0), -1);
? ? ? ? cv::line(resultImg, drawingPoints[drawingPoints.size() - 1], cv::Point(x, y), CV_RGB(0, 0, 255), 1);
? ? ? ? cv::imshow(winName, resultImg);
? ? }
}
void OnMouseEvent(int event, int x, int y, int flags, void* userdata)
{
? ? if (event == cv::EVENT_LBUTTONDOWN)
? ? {
? ? ? ? OnLeftMouseButtonDown(x,y);
? ? }
? ? else if (event == cv::EVENT_RBUTTONDOWN)
? ? {
? ? ? ? OnRightMouseButtonDown(x,y);
? ? }
? ? if (event == cv::EVENT_MOUSEMOVE)
? ? {
? ? ? ? OnMouseMove(x,y);
? ? }
}
?
void OnDirectionKeyDown(short keyCode)
{
? ? int x = keyCode == VK_LEFT ? -2 : (keyCode == VK_RIGHT ? 2 : 0);
? ? int y = keyCode == VK_UP ? -2 : (keyCode == VK_DOWN ? 2 : 0);
? ? maskLocation.x += x;
? ? maskLocation.y += y;
? ? for (int i = 0; i < areaPoints.size(); i++)
? ? {
? ? ? ? areaPoints[i].x += x;
? ? ? ? areaPoints[i].y += y;
? ? }
? ? OnCompleteArea(true);
? ? cv::imshow(winName, resultImg);
}
int main(int argc, char **arv)
{
? ? foregroundImg = cv::imread("test.jpg");
? ? foregroundImg.copyTo(resultImg);
? ? areaMask = cv::Mat::zeros(foregroundImg.size(), CV_8U);
? ? cv::imshow(winName, resultImg);
?
? ? maskLocation.x = maskLocation.y = 0;
? ? cv::setMouseCallback(winName, OnMouseEvent);
? ? int key = cv::waitKeyEx(0);
? ? short lowKey = key;
? ? short highKey = key >> 16;
? ? while (key != VK_ESCAPE)
? ? {
? ? ? ? if (key == VK_RETURN)//Enter
? ? ? ? {
? ? ? ? ? ? OnCompleteArea(false);
? ? ? ? ? ? cv::imshow(winName, resultImg);
? ? ? ? }
? ? ? ? else if (lowKey == 0 && (highKey == VK_UP || highKey == VK_DOWN || highKey == VK_LEFT || highKey == VK_RIGHT))
? ? ? ? {
? ? ? ? ? ? OnDirectionKeyDown(highKey);
? ? ? ? }
? ? ? ? key = cv::waitKeyEx(0);
? ? ? ? lowKey = key;
? ? ? ? highKey = key >> 16;
? ? }
? ? return 0;
}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C語言中循環(huán)嵌套的應用方式

    C語言中循環(huán)嵌套的應用方式

    這篇文章主要介紹了C語言中循環(huán)嵌套的應用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • C語言動態(tài)內(nèi)存分配函數(shù)的實現(xiàn)

    C語言動態(tài)內(nèi)存分配函數(shù)的實現(xiàn)

    這篇文章主要介紹了C語言動態(tài)內(nèi)存分配函數(shù)的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-05-05
  • C/C++?Qt?Tree與Tab組件實現(xiàn)分頁菜單功能

    C/C++?Qt?Tree與Tab組件實現(xiàn)分頁菜單功能

    這篇文章主要介紹了C/C++?Qt?Tree與Tab組件實現(xiàn)分頁菜單功能,實現(xiàn)一個類似于樹形菜單欄的功能,當用戶點擊菜單欄中的選項時則會跳轉(zhuǎn)到不同的頁面上,本文簡單給大家分享實現(xiàn)代碼,感興趣的朋友跟隨小編一起看看吧
    2021-11-11
  • C++日期類計算器的模擬實現(xiàn)舉例詳解

    C++日期類計算器的模擬實現(xiàn)舉例詳解

    兩個日期之間相隔天數(shù)的計算網(wǎng)上有許多的軟件,這里主要介紹如何使用C/C++語言來完成這樣的功能,下面這篇文章主要給大家介紹了關(guān)于C++日期類計算器的模擬實現(xiàn),需要的朋友可以參考下
    2023-04-04
  • 使用VS Code進行Qt開發(fā)的實現(xiàn)

    使用VS Code進行Qt開發(fā)的實現(xiàn)

    這篇文章主要介紹了使用VS Code進行Qt開發(fā)的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-10-10
  • OpenCV實現(xiàn)多圖像拼接成一張大圖

    OpenCV實現(xiàn)多圖像拼接成一張大圖

    這篇文章主要為大家詳細介紹了OpenCV實現(xiàn)多圖像拼接成一張大圖,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • C++中小數(shù)點輸出格式(實例代碼)

    C++中小數(shù)點輸出格式(實例代碼)

    下面小編就為大家?guī)硪黄狢++中小數(shù)點輸出格式(實例代碼)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • c語言實現(xiàn)一個簡單日歷

    c語言實現(xiàn)一個簡單日歷

    本文給大家分享的是一則使用C語言來實現(xiàn)的一個簡單日歷的代碼,根據(jù)項目需求,實現(xiàn)了3個簡單的小功能,推薦給大家,有需要的小伙伴可以參考下。
    2015-03-03
  • C++使用ifstream讀取文件內(nèi)容的示例詳解

    C++使用ifstream讀取文件內(nèi)容的示例詳解

    這篇文章主要為大家詳細介紹了C++如何使用ifstream讀取文件內(nèi)容的功能,文中的示例代碼講解詳細,具有一定的參考價值,感興趣的可以了解一下
    2023-03-03
  • 使用C++的inipp庫處理配置文件.ini的示例詳解

    使用C++的inipp庫處理配置文件.ini的示例詳解

    一個ini文件由多個節(jié)section組成,每個節(jié)由多個鍵值對組成,本文給大家介紹了使用第三方庫inipp來操作ini文件,文中通過代碼示例講解的非常詳細,需要的朋友可以參考下
    2024-01-01

最新評論