cv python 画直线,使用python在打开的cv中使用鼠标事件绘制矩形或直线

I am trying to draw a straight line between two coordinates which would be obtained by clicking on the image or by mouse events. I am able to draw individual circles on clicking the mouse, but cannot figure out how to draw line between those points. When I am using this code, I can only print the start and end coordinates, not draw a line between them.

import numpy as np

import cv2

import cv2.cv as cv

boxes = []

def on_mouse(event, x, y, flags, params):

if event == cv.CV_EVENT_LBUTTONDOWN:

print 'Start Mouse Position: '+str(x)+', '+str(y)

sbox = [x, y]

boxes.append(sbox)

elif event == cv.CV_EVENT_LBUTTONUP:

print 'End Mouse Position: '+str(x)+', '+str(y)

ebox = [x, y]

boxes.append(ebox)

count = 0

while(1):

count += 1

img = cv2.imread('img.jpg',0)

img = cv2.blur(img, (3,3))

cv2.namedWindow('real image')

cv.SetMouseCallback('real image', on_mouse, 0)

cv2.imshow('real image', img)

if count < 50:

if cv2.waitKey(33) == 27:

cv2.destroyAllWindows()

break

elif count >= 50:

if cv2.waitKey(0) == 27:

cv2.destroyAllWindows()

break

count = 0

Somehow, I am not able to extract the coordinates outside the loop. Can someone please suggest how to draw lines or rectangles between the points I click on the image?

解决方案

You can refer below C++ code which I used to crop image

#include

#include "opencv2/opencv.hpp"

#include

using namespace std;

using namespace cv;

Mat src,img,ROI;

Rect cropRect(0,0,0,0);

Point P1(0,0);

Point P2(0,0);

const char* winName="Crop Image";

bool clicked=false;

int i=0;

char imgName[15];

void checkBoundary(){

//check croping rectangle exceed image boundary

if(cropRect.width>img.cols-cropRect.x)

cropRect.width=img.cols-cropRect.x;

if(cropRect.height>img.rows-cropRect.y)

cropRect.height=img.rows-cropRect.y;

if(cropRect.x<0)

cropRect.x=0;

if(cropRect.y<0)

cropRect.height=0;

}

void showImage(){

img=src.clone();

checkBoundary();

if(cropRect.width>0&&cropRect.height>0){

ROI=src(cropRect);

imshow("cropped",ROI);

}

rectangle(img, cropRect, Scalar(0,255,0), 1, 8, 0 );

imshow(winName,img);

}

void onMouse( int event, int x, int y, int f, void* ){

switch(event){

case CV_EVENT_LBUTTONDOWN :

clicked=true;

P1.x=x;

P1.y=y;

P2.x=x;

P2.y=y;

break;

case CV_EVENT_LBUTTONUP :

P2.x=x;

P2.y=y;

clicked=false;

break;

case CV_EVENT_MOUSEMOVE :

if(clicked){

P2.x=x;

P2.y=y;

}

break;

default : break;

}

if(clicked){

if(P1.x>P2.x){ cropRect.x=P2.x;

cropRect.width=P1.x-P2.x; }

else { cropRect.x=P1.x;

cropRect.width=P2.x-P1.x; }

if(P1.y>P2.y){ cropRect.y=P2.y;

cropRect.height=P1.y-P2.y; }

else { cropRect.y=P1.y;

cropRect.height=P2.y-P1.y; }

}

showImage();

}

int main()

{

cout<

cout< Press 's' to save"<

cout< Press '8' to move up"<

cout< Press '2' to move down"<

cout< Press '6' to move right"<

cout< Press '4' to move left"<

cout< Press 'w' increas top"<

cout< Press 'x' increas bottom"<

cout< Press 'd' increas right"<

cout< Press 'a' increas left"<

cout< Press 't' decrease top"<

cout< Press 'b' decrease bottom"<

cout< Press 'h' decrease right"<

cout< Press 'f' decrease left"<

cout< Press 'r' to reset"<

cout< Press 'Esc' to quit"<

src=imread("src.png",1);

namedWindow(winName,WINDOW_NORMAL);

setMouseCallback(winName,onMouse,NULL );

imshow(winName,src);

while(1){

char c=waitKey();

if(c=='s'&&ROI.data){

sprintf(imgName,"%d.jpg",i++);

imwrite(imgName,ROI);

cout<

}

if(c=='6') cropRect.x++;

if(c=='4') cropRect.x--;

if(c=='8') cropRect.y--;

if(c=='2') cropRect.y++;

if(c=='w') { cropRect.y--; cropRect.height++;}

if(c=='d') cropRect.width++;

if(c=='x') cropRect.height++;

if(c=='a') { cropRect.x--; cropRect.width++;}

if(c=='t') { cropRect.y++; cropRect.height--;}

if(c=='h') cropRect.width--;

if(c=='b') cropRect.height--;

if(c=='f') { cropRect.x++; cropRect.width--;}

if(c==27) break;

if(c=='r') {cropRect.x=0;cropRect.y=0;cropRect.width=0;cropRect.height=0;}

showImage();

}

return 0;

}

nZpAJ.jpg

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值