Posted in Python onOctober 24, 2018
做图像识别的时候需要在图片中画出特定大小和角度的矩形框,自己写了一个函数,给定的输入是图片名称,矩形框的位置坐标,长宽和角度,直接输出画好矩形框的图片。
主要思想是先根据x,y坐标和长宽得到矩形,然后通过数学计算得到旋转angle角度后的新矩形框的四个顶点位置坐标,再利用draw.line()函数画出来。
import math import matplotlib.pyplot as plt import numpy as np from PIL import Image, ImageDraw def draw(filename,result): img = Image.open(filename) w,h=img.size draw = ImageDraw.Draw(img) result=np.array(result) x=result[0][0] y=result[0][1] angle=result[0][2] height=result[0][3] width=result[0][4] anglePi = -angle*math.pi/180.0 cosA = math.cos(anglePi) sinA = math.sin(anglePi) x1=x-0.5*width y1=y-0.5*height x0=x+0.5*width y0=y1 x2=x1 y2=y+0.5*height x3=x0 y3=y2 x0n= (x0 -x)*cosA -(y0 - y)*sinA + x y0n = (x0-x)*sinA + (y0 - y)*cosA + y x1n= (x1 -x)*cosA -(y1 - y)*sinA + x y1n = (x1-x)*sinA + (y1 - y)*cosA + y x2n= (x2 -x)*cosA -(y2 - y)*sinA + x y2n = (x2-x)*sinA + (y2 - y)*cosA + y x3n= (x3 -x)*cosA -(y3 - y)*sinA + x y3n = (x3-x)*sinA + (y3 - y)*cosA + y draw.line([(x0n, y0n),(x1n, y1n)], fill=(0, 0, 255)) draw.line([(x1n, y1n),(x2n, y2n)], fill=(255, 0, 0)) draw.line([(x2n, y2n),(x3n, y3n)],fill= (0,0,255)) draw.line([(x0n, y0n), (x3n, y3n)],fill=(255,0,0)) plt.imshow(img) plt.show()
改写成c++
由于c++程序也需要用到这个程序,改写一下
void CopentestDlg::OnBnClickedAnaly() { CString x,y,angle,width; float xRaw[4], yRaw[4]; float xNew[4], yNew[4]; float height = 33; if (imageCut==NULL) { AfxMessageBox(_T("未打开图片不能分析")); return; } GetDlgItem(IDC_EX)->GetWindowText(x); GetDlgItem(IDC_EY)->GetWindowText(y); GetDlgItem(IDC_ANGLE)->GetWindowText(angle); GetDlgItem(IDC_WIDTH)->GetWindowText(width); float xf = _tstof(x); //将Cstring转成float类型 float yf = _tstof(y); float widthf= _tstof(width); float anglef= _tstof(angle); xRaw[1] = xf - 0.5*widthf; yRaw[1] = yf - 0.5*height; xRaw[0] = xf + 0.5*widthf; yRaw[0] = yRaw[1]; xRaw[2] = xRaw[1]; yRaw[2] = yf + 0.5*height; xRaw[3] = xRaw[0]; yRaw[3] = yRaw[2]; float anglePi = -anglef*pi / 180.0; float cosA = cos(anglePi); float sinA = sin(anglePi); xNew[0] = (xRaw[0] - xf)*cosA - (yRaw[0] - yf)*sinA + xf; yNew[0] = (xRaw[0] - xf)*sinA + (yRaw[0] - yf)*cosA + yf; xNew[1] = (xRaw[1] - xf)*cosA - (yRaw[1] - yf)*sinA + xf; yNew[1] = (xRaw[1] - xf)*sinA + (yRaw[1] - yf)*cosA + yf; xNew[2] = (xRaw[2] - xf)*cosA - (yRaw[2] - yf)*sinA + xf; yNew[2] = (xRaw[2] - xf)*sinA + (yRaw[2] - yf)*cosA + yf; xNew[3] = (xRaw[3] - xf)*cosA - (yRaw[3] - yf)*sinA + xf; yNew[3] = (xRaw[3] - xf)*sinA + (yRaw[3] - yf)*cosA + yf; cvLine(imageCut, cvPoint(xNew[0], yNew[0]), cvPoint(xNew[1], yNew[1]), cvScalar(0, 0, 255), 3); cvLine(imageCut, cvPoint(xNew[1], yNew[1]), cvPoint(xNew[2], yNew[2]), cvScalar(255, 0, 0), 3); cvLine(imageCut, cvPoint(xNew[2], yNew[2]), cvPoint(xNew[3], yNew[3]), cvScalar(0, 0, 255), 3); cvLine(imageCut, cvPoint(xNew[0], yNew[0]), cvPoint(xNew[3], yNew[3]), cvScalar(255, 0, 0), 3); DrawPicToHDC(imageCut, IDC_Cut); pEdit->ReplaceSel(_T("图片已修改\r\n")); }
效果图
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。
python实现在图片上画特定大小角度矩形框
- Author -
Imcy声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@