Binarization of images

In the previous article binary image analysis: Case Study (text separation + coin counting) has been introduced, what is the image binarization and the role of binarization.

This time, we use CV4J to implement simple content-based image analysis.

Contour Analysis

A Contours, “Contours,” refers to a curve of the same color or density connecting all continuous points. The work of contour detection is very useful for shape analysis and object detection and recognition.

The complete contour analysis looks like this: The first step is to binarize the image. Of course, Canny can also be directly used to detect edges. In this paper, we use binarization.

        CV4JImage cv4JImage = new CV4JImage(bitmap);
        Threshold threshold = newThreshold(); threshold.process((ByteProcessor)(cv4JImage.convert2Gray().getProcessor()),Threshold.THRESH_OTSU,Threshold.METHOD_THRESH _BINARY,255);
        image1.setImageBitmap(cv4JImage.getProcessor().getImage().toBitmap());Copy the code

Second, connect component tags.

ConnectedAreaLabel connectedAreaLabel = new ConnectedAreaLabel();
        connectedAreaLabel.setFilterNoise(true);
        int[] mask = new int[cv4JImage.getProcessor().getWidth() * cv4JImage.getProcessor().getHeight()];
        connectedAreaLabel.process((ByteProcessor)cv4JImage.getProcessor(),mask,null.false);

        SparseIntArray colors = new SparseIntArray();
        Random random = new Random();

        int height = cv4JImage.getProcessor().getHeight();
        int width = cv4JImage.getProcessor().getWidth();
        int size = height * width;
        for (int i = 0; i<size; i++) {int c = mask[i];
            if (c>=0) {
                colors.put(c, Color.argb(255, random.nextInt(255),random.nextInt(255),random.nextInt(255)));
            }
        }

        cv4JImage.resetBitmap();
        Bitmap newBitmap = cv4JImage.getProcessor().getImage().toBitmap();

        for(int row=0; row<height; row++) {
            for (int col = 0; col < width; col++) {

                int c = mask[row*width+col];
                if (c>=0) {
                    newBitmap.setPixel(col,row,colors.get(c));
                }
            }
        }

        image2.setImageBitmap(newBitmap);Copy the code

The identified connected components are colored, and the colors are randomly generated.

Profile analysis i. JPEG

The third step is contour analysis.

        // Outline analysis
        Bitmap thirdBitmap = Bitmap.createBitmap(newBitmap);
        ContourAnalysis ca = new ContourAnalysis();
        List<MeasureData> measureDatas = new ArrayList<>();
        ca.process((ByteProcessor)(cv4JImage.convert2Gray().getProcessor()),mask,measureDatas);

        Canvas canvas = new Canvas(thirdBitmap);
        Paint paint = new Paint();
        paint.setColor(Color.WHITE);
        for (MeasureData data:measureDatas) {
            canvas.drawText(data.toString(),data.getCp().x,data.getCp().y,paint);
        }
        image3.setImageBitmap(thirdBitmap);Copy the code

Profile analysis II. Jpeg


Zoom in on the result of step 3 to see the detailed description. These include the object’s center of mass, the Angle at which the contour is rotated, the area (the area of pixels), and roundness (a measure of the likelihood that the contour is round)

Profile analysis iii. Jpeg

Print these descriptions to the log.

Print log. Jpeg

ContourAnalysis adopts geometric distance algorithm. Moments are operators describing image features, which are mainly used in image retrieval and recognition, image matching, image reconstruction, digital compression, digital watermarking and moving image sequence analysis.

The first and zero moments are used to calculate the center of gravity of a shape.

First and zero moment. Jpeg


The second moment is used to calculate the direction of the shape.

Second moment. Jpeg





Calculates the direction of the object shape. Jpeg

That’s it. If you’re interested in the ContourAnalysis class, check out the cv4J code.

conclusion

Cv4j is an image processing library hyperyfish and I developed together, with a pure Java implementation, which is still in its early version. This week we fixed some previous bugs. Next week, we’ll start doing histograms.

This series of previous articles: based on the edge retention filter algorithm face grinding algorithm binary image analysis — case combat (text separation + coin counting) Java to achieve gaussian blur and image space convolution Java image filter advanced play Java to achieve the image filter effect