Outlook is introduced

With OpenCV’s ability to do image enhancement, the premise of course is to integrate OpenCV, here does not do the introduction of the integrated environment, the following only introduces the image enhancement way, and each way corresponding to the suitable scene.

The image gray level

According to the logarithmic relationship between white and black is divided into a number of grades, called gray. Gray scale is divided into 256 levels (0 ~ 255, 0: black, 255: white). A grayscale image is called a grayscale image.\

A complete image is composed of red, green and blue channels. The red, green and blue channels are shown in grayscale. Different gray levels are used to represent the proportion of “red, green and blue” in the image. Pure white in the channel represents that the color light is the highest brightness here, and the brightness color order is 255. In contrast, pure black represents the lowest brightness of colored light at that point, and the brightness color level is 0.

To process the image, we actually change the gray value of each pixel to achieve the image we want. Here are four ways to enhance images for different image environments.

Image histogram

The image histogram is the number of each gray value in the image. Then, the gray value of the image is taken as the horizontal axis, and the number of gray value of the image or the proportion of gray value in the image is taken as the vertical axis.

Image enhancement
Image enhancement based on histogram equalization

Histogram equalization is to adjust the gray scale distribution of the image to make the distribution on the gray scale 0~255 more balanced, improve the contrast of the image, to improve the subjective visual effect of the image. Images with low contrast are suitable for histogram equalization to enhance image detail.

fun rectangleEnhance(example: Mat): Mat {// convert if (example.type() == cvtype.cv_8uc4) {imgproc.cvtcolor (example, example, Imgproc.color_bgra2bgr)} val channels = arrayListOf<Mat>() core.split (example, channels) channels.forEachIndexed { _, mat -> Imgproc.equalizeHist(mat, Val DST = mat () Core. Merge (channels, DST) return DST}Copy the code

The effect

Image enhancement based on Laplacian operator

The image can be sharpened and enhanced by convolving the 8 neighborhood Laplacian operator with center 4 with the image, and the Laplacian operator can enhance the local image contrast. Suitable for the overall image contrast is not obvious image enhancement, highlight the local.

fun laplaceEnhance(example: Mat): Mat {// laplaceMat = Mat(3, 3, cvtype.cv_32f) laplaceMat. Put (0, 0, 0.0, -1.0, 0.0, 4.0, 0.0, 0.0, Imgproc.filter2D(example, DST, -1, laplaceMat) return DST}Copy the code

The effect

Enhancement based on log log transformation

Logarithmic transformation can expand the low gray value part of the image to show more details of the low gray value part, and compress the high gray value part to reduce details of the high gray value part, so as to achieve the purpose of emphasizing the low gray value part of the image. For different bases, the larger the base, the stronger the expansion of the low gray part, the stronger the compression of the high gray part.

extern "C" JNIEXPORT void JNICALL Java_com_dream_androidcv_java_NativeActivity_logEnhance(JNIEnv *env, jobject thiz, jobject bitmap_in, jobject bitmap_out) { cv::Mat matBitmapIn; bool toMatrix = BitmapToMatrix(env, bitmap_in, matBitmapIn); if (! toMatrix) return; Mat imageLog(matBitmapIn.size(), CV_32FC3); for (int i = 0; i < matBitmapIn.rows; i++) { for (int j = 0; j < matBitmapIn.cols; J++) {imageLog. At < Vec3f > (I, j) [0] = log (0.5 + matBitmapIn. At < Vec3b > (I, j) [0]). ImageLog. At < Vec3f > (I, j) [1] = log (0.5 + matBitmapIn. At < Vec3b > (I, j) [1]). ImageLog. At < Vec3f > (I, j) [2] = log (0.5 + matBitmapIn. At < Vec3b > (I, j) [2]). } // Normalize to 0~255 normalize(imageLog, imageLog, 0, 255, CV_MINMAX); // Convert to 8bit image display convertScaleAbs(imageLog, imageLog); cvtColor(imageLog,imageLog,COLOR_BGR2RGB); MatrixToBitmap(env, imageLog, bitmap_out); }Copy the code

The effect

Image enhancement based on gamma transform

γ value to 1 as the boundary, the smaller the value, the image of the low gray part of the expansion of the stronger, the larger the value, the image of the high gray part of the expansion of the stronger, through different γ value, you can achieve enhanced low gray or high gray part of the details of the role. The enhancement effect of gamma transform is obvious when the image contrast is low and the overall brightness value is high (for overexposure of the camera).

extern "C" JNIEXPORT void JNICALL Java_com_dream_androidcv_java_NativeActivity_gammaEnhance(JNIEnv *env, jobject thiz, jobject bitmap_in, jobject bitmap_out) { cv::Mat image; bool toMatrix = BitmapToMatrix(env, bitmap_in, image); if (! toMatrix) return; Mat imageGamma(image.size(), CV_32FC3); for (int i = 0; i < image.rows; i++) { for (int j = 0; j < image.cols; j++) { imageGamma.at<Vec3f>(i, j)[0] = (image.at<Vec3b>(i, j)[0])*(image.at<Vec3b>(i, j)[0])*(image.at<Vec3b>(i, j)[0]);  imageGamma.at<Vec3f>(i, j)[1] = (image.at<Vec3b>(i, j)[1])*(image.at<Vec3b>(i, j)[1])*(image.at<Vec3b>(i, j)[1]); imageGamma.at<Vec3f>(i, j)[2] = (image.at<Vec3b>(i, j)[2])*(image.at<Vec3b>(i, j)[2])*(image.at<Vec3b>(i, j)[2]); } // Normalize (imageGamma, imageGamma, 0, 255, CV_MINMAX); // Convert to 8bit image display convertScaleAbs(imageGamma, imageGamma); / / will Mat BGR into RGB cvtColor (imageGamma imageGamma, COLOR_BGR2RGB); MatrixToBitmap(env, imageGamma, bitmap_out); }Copy the code

The effect

conclusion

Not all images are suitable for enhancement. The essence of image enhancement is to smooth the grayscale of the image to achieve the purpose of image enhancement (universal). So for a specific scene, the gray level for a specific processing. For image gray level distribution detection, obtaining an appropriate threshold value is the key to judge whether the image needs to be enhanced.

  • Histogram equalization is suitable for low contrast images to enhance image details.
  • Laplacian convolution is suitable for local enhancement of the overall image with no obvious contrast to highlight local features.
  • Logarithmic transformation is suitable for increasing the brightness of the image, increasing the color level, and compressing the low color level.
  • Gamma transform is suitable for lowering the brightness of the image, compressing high color levels, and pulling up low color levels.