1. 程式人生 > >“毛星雲OpenCV3程式設計入門之python實現”第六篇基本圖形繪製

“毛星雲OpenCV3程式設計入門之python實現”第六篇基本圖形繪製

4.3基本圖形繪製

python程式碼:

# -*- coding: utf-8 -*-
__author__ = 'sunzhilong'


import cv2
import numpy as np

image = np.zeros((600,600,3), dtype=np.uint8)

# 畫橢圓
cv2.ellipse( image,
             (int(image.shape[0]/2), int(image.shape[1]/2)), # 圓心 int型
             (int(image.shape[0]/4) ,int(image.shape[1]/16)), # 長短軸 int型
0.0, # 旋轉角度 double 120.0, # 開始角度 double 360.0, # 結束角度 double (255, 0, 0), # 線條顏色 int (B,G,R) 2, # 線寬 int型 8) # 線型 固定4、8、16 # 畫圓 cv2.circle(image, (300, 300), # 圓心 int型 300, # 半徑 int型
(0, 0, 255), # 線條顏色 (B,G,R) 2, # 線寬 int型 當線寬取值為【負】時,畫出的圓為【實心圓】 4) # 線型 固定4、8、16 # 畫填充多邊形 b = np.array([ [100,100], [250,100], [300,220], [100,230] ], dtype=np.int) cv2.fillPoly(image, [b], # 多邊形點座標 必須帶[],表示陣列,且必須為int型
(0, 255, 0), # 填充顏色 int (B,G,R) 8) # 線型 固定4、8、16 # 畫非填充多邊形 d = np.array([ [400,100], [450,100], [500,220], [300,230] ], dtype=np.int) cv2.polylines(image, [d], # 多邊形點座標 必須帶[],表示陣列,且必須為int型 True, # 布林型,True表示的是線段閉合,False表示的是僅保留線段 (255, 255, 0), # 填充顏色 int (B,G,R) 2, # 線寬 int型 8) # 線型 固定4、8、16 # 畫線 cv2.line(image, (0, 0), # 起始點座標 int (600, 600), # 結束點座標 int (255, 255, 255), # 線條顏色 int 10, # 線寬 int 16) # 線型 固定4、8、16 cv2.imshow("ellipse", image) cv2.waitKey(0)

原書中C++程式碼:

//--------------------------------------【程式說明】-------------------------------------------
//		程式說明:《OpenCV3程式設計入門》OpenCV3版書本配套示例程式20
//		程式描述:使用OpenCV進行基本的繪圖操作
//		開發測試所用作業系統: Windows 7 64bit
//		開發測試所用IDE版本:Visual Studio 2010
//		開發測試所用OpenCV版本:	3.0 beta
//		2014年11月 Created by @淺墨_毛星雲
//		2014年12月 Revised by @淺墨_毛星雲
//------------------------------------------------------------------------------------------------


//---------------------------------【標頭檔案、名稱空間包含部分】----------------------------
//          描述:包含程式所使用的標頭檔案和名稱空間
//------------------------------------------------------------------------------------------------
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;

//此程式對於OpenCV3版需要額外包含標頭檔案:
#include <opencv2/imgproc/imgproc.hpp>



//-----------------------------------【巨集定義部分】-------------------------------------------- 
//		描述:定義一些輔助巨集 
//------------------------------------------------------------------------------------------------ 
#define WINDOW_NAME1 "【繪製圖1】"        //為視窗標題定義的巨集 
#define WINDOW_NAME2 "【繪製圖2】"        //為視窗標題定義的巨集 
#define WINDOW_WIDTH 600//定義視窗大小的巨集



//--------------------------------【全域性函式宣告部分】-------------------------------------
//		描述:全域性函式宣告
//-----------------------------------------------------------------------------------------------
void DrawEllipse( Mat img, double angle );//繪製橢圓
void DrawFilledCircle( Mat img, Point center );//繪製圓
void DrawPolygon( Mat img );//繪製多邊形
void DrawLine( Mat img, Point start, Point end );//繪製線段



//-----------------------------------【ShowHelpText( )函式】----------------------------------
//          描述:輸出一些幫助資訊
//----------------------------------------------------------------------------------------------
void ShowHelpText()
{
	//輸出歡迎資訊和OpenCV版本
	printf("\n\n\t\t\t非常感謝購買《OpenCV3程式設計入門》一書!\n");
	printf("\n\n\t\t\t此為本書OpenCV3版的第20個配套示例程式\n");
	printf("\n\n\t\t\t   當前使用的OpenCV版本為:" CV_VERSION );
	printf("\n\n  ----------------------------------------------------------------------------\n");
}




//---------------------------------------【main( )函式】--------------------------------------
//		描述:控制檯應用程式的入口函式,我們的程式從這裡開始執行
//-----------------------------------------------------------------------------------------------
int main( void )
{

	// 建立空白的Mat影象
	Mat atomImage = Mat::zeros( WINDOW_WIDTH, WINDOW_WIDTH, CV_8UC3 );
	Mat rookImage = Mat::zeros( WINDOW_WIDTH, WINDOW_WIDTH, CV_8UC3 );

	ShowHelpText();
	// ---------------------<1>繪製化學中的原子示例圖------------------------

	//【1.1】先繪製出橢圓
	DrawEllipse( atomImage, 90 );
	DrawEllipse( atomImage, 0 );
	DrawEllipse( atomImage, 45 );
	DrawEllipse( atomImage, -45 );

	//【1.2】再繪製圓心
	DrawFilledCircle( atomImage, Point( WINDOW_WIDTH/2, WINDOW_WIDTH/2) );

	// ----------------------------<2>繪製組合圖-----------------------------
	//【2.1】先繪製出橢圓
	DrawPolygon( rookImage );

	// 【2.2】繪製矩形
	rectangle( rookImage,
		Point( 0, 7*WINDOW_WIDTH/8 ),
		Point( WINDOW_WIDTH, WINDOW_WIDTH),
		Scalar( 0, 255, 255 ),
		-1,
		8 );

	// 【2.3】繪製一些線段
	DrawLine( rookImage, Point( 0, 15*WINDOW_WIDTH/16 ), Point( WINDOW_WIDTH, 15*WINDOW_WIDTH/16 ) );
	DrawLine( rookImage, Point( WINDOW_WIDTH/4, 7*WINDOW_WIDTH/8 ), Point( WINDOW_WIDTH/4, WINDOW_WIDTH ) );
	DrawLine( rookImage, Point( WINDOW_WIDTH/2, 7*WINDOW_WIDTH/8 ), Point( WINDOW_WIDTH/2, WINDOW_WIDTH ) );
	DrawLine( rookImage, Point( 3*WINDOW_WIDTH/4, 7*WINDOW_WIDTH/8 ), Point( 3*WINDOW_WIDTH/4, WINDOW_WIDTH ) );

	// ---------------------------<3>顯示繪製出的影象------------------------
	imshow( WINDOW_NAME1, atomImage );
	moveWindow( WINDOW_NAME1, 0, 200 );
	imshow( WINDOW_NAME2, rookImage );
	moveWindow( WINDOW_NAME2, WINDOW_WIDTH, 200 );

	waitKey( 0 );
	return(0);
}



//-------------------------------【DrawEllipse( )函式】--------------------------------
//		描述:自定義的繪製函式,實現了繪製不同角度、相同尺寸的橢圓
//-----------------------------------------------------------------------------------------
void DrawEllipse( Mat img, double angle )
{
	int thickness = 2;
	int lineType = 8;

	ellipse( img,
		Point( WINDOW_WIDTH/2, WINDOW_WIDTH/2 ),
		Size( WINDOW_WIDTH/4, WINDOW_WIDTH/16 ),
		angle,
		0,
		360,
		Scalar( 255, 129, 0 ),
		thickness,
		lineType );
}


//-----------------------------------【DrawFilledCircle( )函式】---------------------------
//		描述:自定義的繪製函式,實現了實心圓的繪製
//-----------------------------------------------------------------------------------------
void DrawFilledCircle( Mat img, Point center )
{
	int thickness = -1;
	int lineType = 8;

	circle( img,
		center,
		WINDOW_WIDTH/32,
		Scalar( 0, 0, 255 ),
		thickness,
		lineType );
}


//-----------------------------------【DrawPolygon( )函式】--------------------------
//		描述:自定義的繪製函式,實現了凹多邊形的繪製
//--------------------------------------------------------------------------------------
void DrawPolygon( Mat img )
{
	int lineType = 8;

	//建立一些點
	Point rookPoints[1][20];
	rookPoints[0][0]  = Point(    WINDOW_WIDTH/4,   7*WINDOW_WIDTH/8 );
	rookPoints[0][1]  = Point(  3*WINDOW_WIDTH/4,   7*WINDOW_WIDTH/8 );
	rookPoints[0][2]  = Point(  3*WINDOW_WIDTH/4,  13*WINDOW_WIDTH/16 );
	rookPoints[0][3]  = Point( 11*WINDOW_WIDTH/16, 13*WINDOW_WIDTH/16 );
	rookPoints[0][4]  = Point( 19*WINDOW_WIDTH/32,  3*WINDOW_WIDTH/8 );
	rookPoints[0][5]  = Point(  3*WINDOW_WIDTH/4,   3*WINDOW_WIDTH/8 );
	rookPoints[0][6]  = Point(  3*WINDOW_WIDTH/4,     WINDOW_WIDTH/8 );
	rookPoints[0][7]  = Point( 26*WINDOW_WIDTH/40,    WINDOW_WIDTH/8 );
	rookPoints[0][8]  = Point( 26*WINDOW_WIDTH/40,    WINDOW_WIDTH/4 );
	rookPoints[0][9]  = Point( 22*WINDOW_WIDTH/40,    WINDOW_WIDTH/4 );
	rookPoints[0][10] = Point( 22*WINDOW_WIDTH/40,    WINDOW_WIDTH/8 );
	rookPoints[0][11] = Point( 18*WINDOW_WIDTH/40,    WINDOW_WIDTH/8 );
	rookPoints[0][12] = Point( 18*WINDOW_WIDTH/40,    WINDOW_WIDTH/4 );
	rookPoints[0][13] = Point( 14*WINDOW_WIDTH/40,    WINDOW_WIDTH/4 );
	rookPoints[0][14] = Point( 14*WINDOW_WIDTH/40,    WINDOW_WIDTH/8 );
	rookPoints[0][15] = Point(    WINDOW_WIDTH/4,     WINDOW_WIDTH/8 );
	rookPoints[0][16] = Point(    WINDOW_WIDTH/4,   3*WINDOW_WIDTH/8 );
	rookPoints[0][17] = Point( 13*WINDOW_WIDTH/32,  3*WINDOW_WIDTH/8 );
	rookPoints[0][18] = Point(  5*WINDOW_WIDTH/16, 13*WINDOW_WIDTH/16 );
	rookPoints[0][19] = Point(    WINDOW_WIDTH/4,  13*WINDOW_WIDTH/16 );

	const Point* ppt[1] = { rookPoints[0] };
	int npt[] = { 20 };

	fillPoly( img,
		ppt,
		npt,
		1,
		Scalar( 255, 255, 255 ),
		lineType );
}


//-----------------------------------【DrawLine( )函式】--------------------------
//		描述:自定義的繪製函式,實現了線的繪製
//---------------------------------------------------------------------------------
void DrawLine( Mat img, Point start, Point end )
{
	int thickness = 2;
	int lineType = 8;
	line( img,
		start,
		end,
		Scalar( 0, 0, 0 ),
		thickness,
		lineType );
}