Basic Image Processing Sample

The basic image processing sample script is located in the ucp_tutorial/vp/vp_samples/script/01_basic_processing directory. The sample shows how to perform related processing on the picture, including picture filtering, picture morphological processing, picture equalization, the detailed implementation method, please combine with the sample source code to compare the practice.

In this sample, direct execution of the sample script performs the default picture processing flow: filtering with sepFilter2D, detecting morphological edges, and picture equalization. If you want to change the processing flow of the picture, you can control the flow of the execution by appending parameter when executing the sample script, the rules for appending parameters are as follows:

sh run_basic_process.sh [filter type] [morphology type]

Included among these:

  • filter type: optional parameter, filter operator.
  • morphology type: optional parameter, morphological operatio.

A list of all available append parameters can also be obtained with the append -help command.

The result of the picture processing is saved in the vp_samples/script/01_basic_processing directory after the algorithm is executed, and the contents of the generated object for this sample are as follows:

vp_samples └── script // Picture data catalog └── 01_basic_processing ├── filtered_image.jpg // Filtered picture ├── morphology_image.jpg // Morphological transformed picture └── equalizeHist_image.jpg // Histogram equalized picture

The execution flow of the sample is shown below:

VP6_01Flow

As shown in the execution flowchart of the sample, the processing of the picture by the sample can be roughly divided into the following four stages:

  1. Filtering of the noised picture. According to the different filtering algorithms, they can be categorized into bilinear filtering, box filtering, gaussian filtering, median filtering, two-dimensional filtering, and separated two-dimensional filtering.

  2. Morphological processing of filtered picture. According to the processing methods can be categorized as corrosion, expansion, open operation, closed operation, and detection of edges.

  3. Histogram equalization of morphologically processed pictures.

Image filter

In the image filter section, the sample encapsulates the six filtering algorithms in the corresponding functions, and uses std::map to establish the connection between the strings and the filtering functions. As shown below, the parameter filter_op holds the mapping between the keywords and the functions.

typedef int32_t (*example_fn)(hbVPImage &src, hbVPImage &dst); static std::map<std::string, example_fn> filter_op = { {"bilateralFilter", bilateral_filter}, {"boxFilter", box_filter}, {"filter2D", filter2D}, {"gaussianBlur", gaussian_blur}, {"medianBlur", median_blur}, {"sepFilter2D", sep_filter2D}, {"default", sep_filter2D}};

During the execution of the sample code, according to the different append keywords in the argv parameter, it is automatically adapted to the corresponding filtering algorithm. The core logic is as follows:

/**=============================================================== * process noise image with filter operate * ===============================================================*/ if (argc > 1) { ret = filter_op[argv[1]](src, dst); LOGE_AND_RETURN_IF(0 != ret, HB_UCP_INVALID_ARGUMENT, "filter operate failed"); } else { ret = filter_op["default"](src, dst); LOGE_AND_RETURN_IF(0 != ret, HB_UCP_INVALID_ARGUMENT, "filter operate failed"); }

Take sepFilter2D filtering algorithm as an example, the processing effect on the noise-added pictures is shown as belows:

Before filtering:

image

After filtering:

image

Morphological process

Morphological processing operators include erosion, dilation, open and close operations, top-hat, black-hat, and so on. In this example, by calling the hbVPDilate and hbVPErode interfaces. This example encapsulates some of these morphological functions, please check the function source code for the specific implementation. Similar to the filtering session, the morphology_op parameter is created here to store the link between the string and the morphology function. The code is as follows:

static std::map<std::string, example_fn> morphology_op = { {"dilate", dilate}, {"erode", erode}, {"open", open}, {"close", close}, {"edge", edge}, {"default", edge}};

The same string adaptation is used for scheduling morphological functions, and the core logic is as follows:

/**=============================================================== * morphology operate * ===============================================================*/ if (argc > 2) { ret = morphology_op[argv[2]](src, dst); LOGE_AND_RETURN_IF(0 != ret, HB_UCP_INVALID_ARGUMENT, "morphology operate failed"); } else { ret = morphology_op["default"](src, dst); LOGE_AND_RETURN_IF(0 != ret, HB_UCP_INVALID_ARGUMENT, "morphology operate failed"); }

Taking the morphological gradient edge algorithm (the edge function in the example) as an example, the effect on the filtered image is shown as belows:

Filtered picture:

image

Morphological gradient edge detection picture:

image

Histogram equalization

In order to enhance the contrast and bring out the details of the picture, the sample performs histogram equalization on the morphology processed picture through the equalizeHist function. The equalization effect is shown as belows:

Original picture:

image

Equalized effect picture:

image