当前位置:网站首页>Halcon: check of blob analysis_ Blister capsule detection
Halcon: check of blob analysis_ Blister capsule detection
2022-07-05 08:40:00 【Aii parson】
* This example demonstrates an application from the pharmaceutical industry.
* The task is to check the content of automatically filled blisters.
* The first image (reference) is used to locate the chambers within a blister shape as a reference model, which is then used to realign the subsequent images along to this reference shape. Using
blob analysis the content of each chamber is segmented and finally classified by a few shape features.
* Use blob analysis , The content of each chamber is segmented and finally classified by some shape features .
*1) Take images
dev_close_window ()
dev_update_off ()
read_image (ImageOrig, 'blister/blister_reference')
dev_open_window_fit_image (ImageOrig, 0, 0, -1, -1, WindowHandle) * Open a new drawing window with a given minimum and maximum range , In order to preserve the aspect ratio of a given image size
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
dev_set_draw ('margin')
dev_set_line_width (3)
* 2) location ( Find the standard position ,blob)
* In the first step, we create a pattern to cut out the chambers in the
* subsequent blister images easily.
access_channel (ImageOrig, Image1, 1) * Get a grayscale image ( The first channel Image )
threshold (Image1, Region, 90, 255)
shape_trans (Region, Blister, 'convex')
orientation_region (Blister, Phi) *orientation_region Calculate the direction of the area , This operator is based on elliptic_axis From the operator ,elliptic_axis Is to find the equivalent ellipse , So use operators orientation_region It's equivalent to putting the area Blister Into an equivalent ellipse , Calculate the angle of the long axis of this ellipse in the image Phi.
area_center (Blister, Area1, Row, Column)
vector_angle_to_rigid (Row, Column, Phi, Row, Column, 0, HomMat2D)# The rigid affine transformation is calculated according to the point correspondence and two corresponding angles , That is, affine transformation composed of rotation and Translation
affine_trans_image (ImageOrig, Image2, HomMat2D, 'constant', 'false')*affine_trans_image - Make arbitrary 2D Affine transformation .ImageOrig: The image to be affine transformed ;Image2: Image after affine transformation ;HomMat2D:2D Affine transformation matrix ;constant: The way of affine transformation , This represents the use of mean filters to prevent aliasing .false: After affine transformation, the area with the size of the image will not be cut out ; if true On the contrary .
gen_empty_obj (Chambers)* Generate an empty object Chambers, Be similar to list.append()
* Draw area squares
for I := 0 to 4 by 1
Row := 88 + I * 70
for J := 0 to 2 by 1
Column := 163 + J * 150
gen_rectangle2 (Rectangle, Row, Column, 0, 64, 30)* Generate a rotatable rectangle ; * * The first two parameters are the coordinates of the center of the region , The third parameter is the rectangle angle , The last two parameters are the width and height of the rectangle .
concat_obj (Chambers, Rectangle, Chambers)*concat_obj (Chambers, Rectangle, Chambers) * concat_obj - Blend the two objects together . Be careful , this And union Dissimilarity ,union Is to integrate two objects into one object , The number of elements of the integrated object is 1; While using concat_obj It is to combine several objects into one object , The sum of the number of elements in this object does not change .
endfor
endfor
affine_trans_region (Blister, Blister, HomMat2D, 'nearest_neighbor')* Use transformation matrix HomMat2D Yes Blister Area use nearest_neighbor methods Affine transformation , there Blister It is affine transformed Blister.
difference (Blister, Chambers, Pattern)* Find the region Blister And region Chambers The difference between the set

union1 (Chambers, ChambersUnion)* Put the area Chambers( There are multiple elements ) Unite into a region ( An element )
orientation_region (Blister, PhiRef)
PhiRef := rad(180) + PhiRef
area_center (Blister, Area2, RowRef, ColumnRef)
*
*
* Each image read will be aligned to this pattern and reduced to the area of interest,
* which is the chambers of the blister
Cycle through each picture
Count := 6
for Index := 1 to Count by 1
read_image (Image, 'blister/blister_' + Index$'02')
threshold (Image, Region, 90, 255)
connection (Region, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 5000, 9999999)
shape_trans (SelectedRegions, RegionTrans, 'convex')
*
* Align pattern along blister of image
orientation_region (RegionTrans, Phi)
area_center (RegionTrans, Area3, Row, Column)
vector_angle_to_rigid (Row, Column, Phi, RowRef, ColumnRef, PhiRef, HomMat2D)
affine_trans_image (Image, ImageAffineTrans, HomMat2D, 'constant', 'false')
*
* Segment pills
reduce_domain (ImageAffineTrans, ChambersUnion, ImageReduced)* Use area to cut pictures , Reduce the definition domain of image processing
decompose3 (ImageReduced, ImageR, ImageG, ImageB)* The picture ImageReduced Divide into R/G/B Three channel image
var_threshold (ImageB, Region, 7, 7, 0.2, 2, 'dark')* The image is threshold processed by local mean and standard deviation analysis
connection (Region, ConnectedRegions0)
closing_rectangle1 (ConnectedRegions0, ConnectedRegions, 3, 3)* Closed operations can fill up small lakes ( It's a small hole ), Close the little cracks , And the overall position and shape remain unchanged .
fill_up (ConnectedRegions, RegionFillUp)
select_shape (RegionFillUp, SelectedRegions, 'area', 'and', 1000, 99999)
opening_circle (SelectedRegions, RegionOpening, 4.5)
connection (RegionOpening, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 1000, 99999)
shape_trans (SelectedRegions, Pills, 'convex')


*
* Classify segmentation results and display statistics
count_obj (Chambers, Number)
gen_empty_obj (WrongPill)
gen_empty_obj (MissingPill)
for I := 1 to Number by 1
select_obj (Chambers, Chamber, I)
intersection (Chamber, Pills, Pill)
area_center (Pill, Area, Row1, Column1)
if (Area > 0)
min_max_gray (Pill, ImageB, 0, Min, Max, Range)
if (Area < 3800 or Min < 60)
concat_obj (WrongPill, Pill, WrongPill)
endif
else
concat_obj (MissingPill, Chamber, MissingPill)
endif
endfor
*
dev_clear_window ()
dev_display (ImageAffineTrans)
dev_set_color ('forest green')
count_obj (Pills, NumberP)
count_obj (WrongPill, NumberWP)
count_obj (MissingPill, NumberMP)
dev_display (Pills)
if (NumberMP > 0 or NumberWP > 0)
disp_message (WindowHandle, 'Not OK', 'window', 12, 12 + 600, 'red', 'true')
else
disp_message (WindowHandle, 'OK', 'window', 12, 12 + 600, 'forest green', 'true')
endif
*
Message := '# Correct pills: ' + (NumberP - NumberWP)
Message[1] := '# Wrong pills : ' + NumberWP
Message[2] := '# Missing pills: ' + NumberMP
*
Colors := gen_tuple_const(3,'black')
if (NumberWP > 0)
Colors[1] := 'red'
endif
if (NumberMP > 0)
Colors[2] := 'red'
endif
disp_message (WindowHandle, Message, 'window', 12, 12, Colors, 'true')
dev_set_color ('red')
dev_display (WrongPill)
dev_display (MissingPill)
if (Index < Count)
disp_continue_message (WindowHandle, 'black', 'true')
endif
stop ()
endfor
边栏推荐
- STM32 --- configuration of external interrupt
- Infix expression evaluation
- Is the security account given by Yixue school safe? Where can I open an account
- leetcode - 445. Add two numbers II
- Apaas platform of TOP10 abroad
- 第十八章 使用工作队列管理器(一)
- Typical low code apaas manufacturer cases
- Guess riddles (2)
- 猜谜语啦(6)
- MATLAB skills (28) Fuzzy Comprehensive Evaluation
猜你喜欢

Arduino operation stm32
Example 001: the number combination has four numbers: 1, 2, 3, 4. How many three digits can be formed that are different from each other and have no duplicate numbers? How many are each?
实例001:数字组合 有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?

Illustration of eight classic pointer written test questions

Daily question - input a date and output the day of the year

猜谜语啦(2)

How apaas is applied in different organizational structures

剑指 Offer 06. 从尾到头打印链表
![[three tier architecture]](/img/73/c4c75a453f03830e83cabb0762eb9b.png)
[three tier architecture]

Guess riddles (6)
随机推荐
猜谜语啦(2)
TypeScript手把手教程,简单易懂
An enterprise information integration system
Infix expression evaluation
第十八章 使用工作队列管理器(一)
Guess riddles (3)
Run menu analysis
[daily training -- Tencent selected 50] 557 Reverse word III in string
猜谜语啦(3)
The first week of summer vacation
Bluebridge cup internet of things competition basic graphic tutorial - clock selection
Guess riddles (8)
剑指 Offer 05. 替换空格
Guess riddles (7)
Example 001: the number combination has four numbers: 1, 2, 3, 4. How many three digits can be formed that are different from each other and have no duplicate numbers? How many are each?
STM32 --- GPIO configuration & GPIO related library functions
Example 009: pause output for one second
Typical low code apaas manufacturer cases
Speech recognition learning summary
Matlab tips (28) fuzzy comprehensive evaluation