How to Read Barcodes with Imbalanced Colour
As we all know, any color is composed of red, blue and green (RGB). If the image is a colour image, Dynamsoft Barcode Reader (DBR) will convert it to grayscale first for further processing.
Gray = Red*RedChannelWeight + Green*GreenChannelWeight + Blue*BlueChannelWeight
The default weights in DBR are suitable for most cases. However, some color images may produce poor grayscale images based on the default weights. For example, the image below is a color image with imbalanced colour.
Figure 1 – Sample barcode colour image
The image below shows the grayscale image converted by DBR using the default channel weights setting.
Figure 2 – Sample barcode gray image using default mode
The image below shows the grayscale image converted by DBR using only red channel.
Figure 3 – Sample barcode gray image using only red channel
As we can see, the gray image converted using only red channel is much better than the default one. Now we will demonstrate how to configure the parameter ColourConversionModes
to adjust the colour channel weights used for converting a colour image to a grayscale image.
Particular Parameter Required
DBR provides a parameter ColourConversionModes
that allows you to control the conversion of a color image to grayscale.
Sample Code
Below is an example illustrating how to configure the parameter ColourConversionModes
to use only red channel.
-
update parameter
ColourConversionModes
in your JSON template{ "CaptureVisionTemplates": [ { "Name": "CV_0", "ImageROIProcessingNameArray": ["TA_0" ] } ], "TargetROIDefOptions" : [ { "Name": "TA_0", "TaskSettingNameArray": [ "BR_0" ] } ], "BarcodeReaderTaskSettingOptions": [ { "Name" : "BR_0", "SectionImageParameterArray": [ { "Section": "ST_REGION_PREDETECTION", "ImageParameterName": "IP_0" }, { "Section": "ST_BARCODE_LOCALIZATION", "ImageParameterName": "IP_0" }, { "Section": "ST_BARCODE_DECODING", "ImageParameterName": "IP_0" } ] } ], "ImageParameterOptions": [ { "Name": "IP_0", "ColourConversionModes": [ { "Mode": "CICM_GENERAL", "RedChannelWeight": 1000, "GreenChannelWeight": 0, "BlueChannelWeight": 0 } ] } ] }
-
apply settings by calling method
InitSettingsFromFile
- JavaScript
- C++
- Android
- Objective-C
- Swift
// `router` is an instance of `CaptureVisionRouter`. // In the JS edition, the method name we use for initialization is different. router.initSettings("PATH-TO-YOUR-SETTING")
char szErrorMsg[256] = {0}; CCaptureVisionRouter* cvr = new CCaptureVisionRouter; cvr->InitSettingsFromFile("PATH-TO-YOUR-SETTING-FILE", szErrorMsg, 256); // more process here
try { // `cvr` is an instance of `CaptureVisionRouter`. cvr.initSettingsFromFile("PATH-TO-YOUR-SETTING-FILE"); } catch (CaptureVisionRouterException e) { e.printStackTrace(); }
NSError *error; // `cvr` is an instance of `DSCaptureVisionRouter`. [self.cvr initSettingsFromFile:@"PATH-TO-YOUR-SETTING-FILE" error:&error];
do{ //`cvr` is an instance of `CaptureVisionRouter`. try cvr.initSettingsFromFile("PATH-TO-YOUR-SETTING-FILE") }catch{ // Add code to do when error occurs. }