Halcon知识:如何画出灰度图像直方图
生活随笔
收集整理的這篇文章主要介紹了
Halcon知识:如何画出灰度图像直方图
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、說明
????????halcon的數據可視化不是很發達。因此,做出數據表、曲線等,需要自己開發。本文講如何顯示灰度圖的直方圖。
?
二、代碼實現
2.1 顯示原圖和直方圖代碼
此為主調用函數,包括讀入原圖、生成直方圖、顯示直方圖。
read_image (Image, 'D:/images/maps/david.jpg') get_image_size (Image, Width, Height) dev_open_window (0, 0, Width, Height, 'black', WindowHandleImage) disp_image(Image, WindowHandleImage) dev_open_window (0, Width + 5, Width, Height, 'black', WindowHandleHisto) gray_histo(Image,Image, AbsoluteHisto, RelativeHisto)dev_set_window (WindowHandleHisto) dev_clear_window () dev_set_draw ('fill') Min:=0 Max:=255 HistoBorder := 0.2 plot_histo( AbsoluteHisto, WindowHandleHisto, 'Gray values', Min, Max, 'white', 'dim gray', HistoBorder)其中,plot_histo是自定義函數,下面介紹如何生成這個自定義函數。
2.2 自定義函數plot_histo
第一步:打開自定義函數菜單
?第二步:輸入函數名
第三步、輸入參數
?
?第四步、將以下代碼粘入
* This procedure plots the Histogram within the value range * [LabelXMin,LabelXMax] into the WindowHandle by leaving a * relative Border around the plot. * if (|Histogram| <= 0 or Border < 0 or Border >= 1)disp_message (WindowHandle, 'Failed to plot histogram', 'window', 12, 12, 'black', 'true')return () endif * * Determine the area to be used for the plot. get_part (WindowHandle, Row1, Column1, Row2, Column2) PlotAreaX := (Column2 - Column1 + 1) * (1 - Border * 2) PlotAreaY := (Row2 - Row1 + 1) * (1 - Border * 2) OriginX := Column1 + Border * (Column2 - Column1 + 1) OriginY := Row2 - Border * (Row2 - Column1 + 1) * * Determine the extent of the function to be plotted MinY := min([Histogram,0]) MaxY := max(Histogram) * * Determine the scaling values ScaleX := PlotAreaX / |Histogram| if (MaxY == MinY)ScaleY := PlotAreaYMaxY := '' elseScaleY := PlotAreaY / (MaxY - MinY) endif * * Display the histogram values polyX := [] polyY := [] for Value := 0 to |Histogram| - 1 by 1polyX := [polyX,OriginX + Value * ScaleX]polyY := [polyY,OriginY - (Histogram[Value] - MinY) * ScaleY]polyX := [polyX,OriginX + (Value + 1) * ScaleX]polyY := [polyY,OriginY - (Histogram[Value] - MinY) * ScaleY] endfor polyX := [polyX,OriginX + |Histogram| * ScaleX,OriginX] polyY := [polyY,OriginY,OriginY] dev_set_window (WindowHandle) dev_set_line_width (1) dev_set_color (ColorGraph) gen_region_polygon_filled (Histo, polyY, polyX) dev_display (Histo) * * Display the coordinate system dev_set_line_width (1) dev_set_color (ColorAxis) gen_arrow_contour_xld (ArrowX, OriginY, OriginX, OriginY, OriginX + PlotAreaX, 5, 5) gen_arrow_contour_xld (ArrowY, OriginY, OriginX, OriginY - PlotAreaY, OriginX, 5, 5) dev_display (ArrowX) dev_display (ArrowY) * dev_set_color (ColorAxis) get_string_extents (WindowHandle, LabelX, Ascent, Descent, W, H) set_tposition (WindowHandle, OriginY + 10, OriginX + PlotAreaX / 2 - W / 2) write_string (WindowHandle, LabelX) * if (int(ValueXMin) == ValueXMin)MinXD := int(ValueXMin) elseMinXD := ValueXMin endif get_string_extents (WindowHandle, MinXD$'3.1f', Ascent1, Descent1, W, H) set_tposition (WindowHandle, OriginY + 10, OriginX) write_string (WindowHandle, MinXD$'3.1f') if (int(ValueXMax) == ValueXMax)MaxXD := int(ValueXMax) elseMaxXD := ValueXMax endif get_string_extents (WindowHandle, MaxXD$'3.1f', Ascent2, Descent2, W, H) set_tposition (WindowHandle, OriginY + 10, OriginX + PlotAreaX - W) write_string (WindowHandle, MaxXD$'3.1f') get_string_extents (WindowHandle, MinY, Ascent3, Descent3, W, H) set_tposition (WindowHandle, OriginY - H, OriginX - 10 - W) write_string (WindowHandle, MinY) get_string_extents (WindowHandle, MaxY, Ascent4, Descent4, W, H) set_tposition (WindowHandle, OriginY - PlotAreaY, OriginX - 10 - W) write_string (WindowHandle, MaxY) * return ()?注意:需要將調用文件和自定義函數文件放在一個路徑就可以調用。
總結
以上是生活随笔為你收集整理的Halcon知识:如何画出灰度图像直方图的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Halcon算子:min_max_gra
- 下一篇: halcon知识:圆度和紧凑度