Halcon例程分析8:投影變換矯正傾斜圖像

打開halcon,按下ctrl+e打開halcon自帶例程。方法->二維碼識別->2d_data_codesrectify_symbol.hdev

*This program demonstrates how to read a slanted
* 2d data code by preprocessing with rectification
*
dev_update_off()
dev_close_window()
* Get the image and display it
*讀取並顯示圖像
read_image(Image_slanted, 'datacode/ecc200/ecc200_to_preprocess_001')
dev_open_window_fit_image(Image_slanted, 0, 0, -1, -1, WindowHandle)
set_display_font(WindowHandle, 14, 'mono', 'true', 'false')
*設置畫筆顏色與寬度
dev_set_color('green')
dev_set_line_width(3)
*定義一些顯示信息變量
Message : = 'This program demonstrates how to preprocess'
	Message[1] : = 'a slanted 2d data code with rectification'
	Message[2] : = 'before reading the data code symbol.'
	*顯示信息
	disp_message(WindowHandle, Message, 'window', 12, 12, 'black', 'true')
	disp_continue_message(WindowHandle, 'black', 'true')
	stop()
	* Initialize coordinates
	*定義座標變量,這是需要進行變換的原始圖像的四個邊角座標,在實際項目中這些數據可以通過blob分析或其他方式獲取到
	XCoordCorners : = [130, 225, 290, 63]
	YCoordCorners : = [101, 96, 289, 269]
	*
	* Display the slanted image and the corners of the symbol
	*生成十字叉,方便顯示在圖像上
	*第一個參數輸出變量
	*第二個參數十字的X座標
	*第三個參數十字的Y座標
	*第四個參數十字叉大小
	*第五個參數十字叉的方向
	gen_cross_contour_xld(Crosses, XCoordCorners, YCoordCorners, 6, 0.785398)
	*顯示
	dev_display(Image_slanted)
	dev_display(Crosses)
	disp_message(WindowHandle, 'Slanted image', 'window', 12, 12, 'black', 'true')
	Message : = 'The marked corners are used to generate a'
	Message[1] : = 'homogeneous transformation matrix which'
	Message[2] : = 'defines the projective transformation for'
	Message[3] : = 'the rectification of the symbol.'
	disp_message(WindowHandle, Message, 'window', 380, 12, 'black', 'true')
	disp_continue_message(WindowHandle, 'black', 'true')
	stop()
	*
	* First generate a transformation matrix using the given points
	* of the corners of the data code symbol and the corresponding points
	* of a quare.
	*求出投影變換矩陣
	*第一個參數原圖像的X座標
	*第二個參數原圖像的Y座標
	*第三個參數原圖像的W座標,因爲是在同一個平面上,全設置爲1,
	*第四個參數目標圖像上的X座標
	*第五個參數目標圖像上的Y座標
	*第六個參數目標圖像上的W座標
	*第七個參數變換方式選擇,對於常規幾何問題,選擇'normalized_dlt'
	*第八個參數輸出的投影變化矩陣
	hom_vector_to_proj_hom_mat2d(XCoordCorners, YCoordCorners, [1, 1, 1, 1], [70, 270, 270, 70], [100, 100, 300, 300], [1, 1, 1, 1], 'normalized_dlt', HomMat2D)
	*
	* Now rectifiy the slanted image by applying the projective transformation
	*對原圖像進行投影變換
	*第一個參數輸入的傾斜圖像
	*第二個參數輸出矯正後的圖像
	*第三個參數求出的投影變換矩陣
	*第四個參數投影后的圖像插值方式
	*第五個參數是否適應圖像大小,false是變換圖跟原圖一樣大小
	*第五個參數是否對原圖像所有像素進行投影變換
	projective_trans_image(Image_slanted, Image_rectified, HomMat2D, 'bilinear', 'false', 'true')
	*
	* Create the data code model and search
	* for the data code in the rectified image
	create_data_code_2d_model('Data Matrix ECC 200', [], [], DataCodeHandle)
	*尋找二維碼輪廓與解析二維碼數據
	find_data_code_2d(Image_rectified, SymbolXLDs, DataCodeHandle, [], [], ResultHandles, DecodedDataStrings)
	*
	* Display result
	dev_display(Image_slanted)
	dev_display(Image_rectified)
	dev_display(SymbolXLDs)
	disp_message(WindowHandle, 'Decoding successful ', 'window', 12, 12, 'black', 'true')
	set_display_font(WindowHandle, 12, 'mono', 'true', 'false')
	disp_message(WindowHandle, DecodedDataStrings, 'window', 350, 70, 'forest green', 'true')





測試用於矯正的原圖像

較矯正後測試的二維碼圖像

 

上面的官方的例子個人感覺不是很直觀,下面是我自己寫的一個例程,矯正一個放置傾斜的二維碼,思路跟上面是完全一樣的


    dev_update_on()
	dev_close_window()
	* Get the image and display it
	*讀取並顯示圖像
	read_image(Image_slanted, 'C:/Users/Administrator/Desktop/6.jpg')
	dev_open_window_fit_image(Image_slanted, 0, 0, -1, -1, WindowHandle)
	set_display_font(WindowHandle, 14, 'mono', 'true', 'false')
	dev_display(Image_slanted)
	stop()
	*blob分析找到二維碼的位置
	create_data_code_2d_model('QR Code', [], [], DataCodeHandle)
	find_data_code_2d(Image_slanted, SymbolXLDs, DataCodeHandle, [], [], ResultHandles, DecodedDataStrings)
	dev_display(Image_slanted)
	dev_display(SymbolXLDs)
	get_contour_xld(SymbolXLDs, Rows, Cols)
	Rows: = remove(Rows, 4)
	Cols : = remove(Cols, 4)
	*
	* First generate a transformation matrix using the given points
	* of the corners of the data code symbol and the corresponding points
	* of a quare.
	*求出投影變換矩陣
	*第一個參數原圖像的X座標
	*第二個參數原圖像的Y座標
	*第三個參數原圖像的W座標,因爲是在同一個平面上,全設置爲1,
	*第四個參數目標圖像上的X座標
	*第五個參數目標圖像上的Y座標
	*第六個參數目標圖像上的W座標
	*第七個參數變換方式選擇,對於常規幾何問題,選擇'normalized_dlt'
	*第八個參數輸出的投影變化矩陣
	hom_vector_to_proj_hom_mat2d(Rows, Cols, [1, 1, 1, 1], [100, 250, 250, 100], [250, 250, 100, 100], [1, 1, 1, 1], 'normalized_dlt', HomMat2D)
	*
	* Now rectifiy the slanted image by applying the projective transformation
	*對原圖像進行投影變換
	*第一個參數輸入的傾斜圖像
	*第二個參數輸出矯正後的圖像
	*第三個參數求出的投影變換矩陣
	*第四個參數投影后的圖像插值方式
	*第五個參數是否適應圖像大小,false是變換圖跟原圖一樣大小
	*第五個參數是否對原圖像所有像素進行投影變換
	projective_trans_image(Image_slanted, Image_rectified, HomMat2D, 'bilinear', 'false', 'true')
	*
	* Create the data code model and search
	* for the data code in the rectified image
	create_data_code_2d_model('QR Code', [], [], DataCodeHandle)
	*尋找二維碼輪廓與解析二維碼數據
	find_data_code_2d(Image_rectified, SymbolXLDs, DataCodeHandle, [], [], ResultHandles, DecodedDataStrings)
	*
	* Display result
	dev_display(Image_rectified)
	dev_display(SymbolXLDs)
	disp_message(WindowHandle, 'Decoding successful ', 'window', 12, 12, 'black', 'true')
	set_display_font(WindowHandle, 12, 'mono', 'true', 'false')
	disp_message(WindowHandle, DecodedDataStrings, 'window', 350, 70, 'forest green', 'true')

待矯正圖像原圖

矯正效果圖

 

發佈了60 篇原創文章 · 獲贊 8 · 訪問量 9688
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章