Opencv Python method for adjusting image contrast and background bleaching of white paper photos with text

LaoYuanPython 2021-10-29 16:50:53
opencv python method adjusting image

One 、 introduction

Previously, we introduced that histogram equalization can adjust the image contrast , So is there any other way to adjust the contrast ? The answer is yes , Let's recruit hard core today .

Two 、 Hard core method for contrast adjustment

This hard core method is the threshold processing of reference gray image , We know that the basic idea of threshold processing of gray image is :

The gray value in the image is greater than or equal to the specified threshold value , Set it to 255, Less than this value is set to 0.

However, the threshold processing results in a binary graph , For contrast enhancement, it must be adjusted . It could be changed as follows :
The gray value in the image is greater than or equal to the specified threshold value , Set its gray value to the original gray value +x, Less than this value is set to the original gray value -x. among x Set a fixed value to adjust the contrast .

3、 ... and 、 The realization of adjustment

When shooting white paper with a digital camera , The resulting image is not really white paper , It's a light gray picture with uneven gray , This situation needs to be adjusted to get a more appropriate background and prospect . Next, take a white paper photo and a picture pasted with text as the processing object , The picture is as follows :
 Insert picture description here

The following is used OpenCV-Python To implement the above algorithm :

import numpy as np
import cv2
from opencvPublic import preparePreviewImg,previewImgList,readImgFile
def adjustImgContrast():
img = readImgFile(r'f:\pic\paper.jpg',True)
bigmask = cv2.compare(img, np.uint8([127]), cv2.CMP_GE)
smallmask = cv2.bitwise_not(bigmask)
x = 90
big = cv2.add(img, x, mask=bigmask)
small = cv2.subtract(img, x, mask=smallmask)
res = cv2.add(big, small)
preparePreviewImg(' The input image : White paper photo ', img, fontSize=38, color=(255, 255, 255))
preparePreviewImg('https://blog.csdn.net/LaoYuanPython', None, fontSize=38, color=(255, 255, 255))
preparePreviewImg(' Adjust contrast photos ', res, fontSize=38, color=(255, 0, 0))
previewImgList()
print('\033[31m Old ape Python website :\033[34mhttps://blog.csdn.net/LaoYuanPython\033[0m, WeChat official account :\033[34m Old ape Python')

The above implementation uses compare function , It can also be processed directly pixel by pixel .
Let's look at the results :
 Insert picture description here

It can be seen that the effect of the processed image is much better than that of the original image , Of course, you can continue to turn it up x It's worth better results .

In the above code ,opencvPublic It is a common module of image processing custom methods commonly used by old apes , The custom common module function used in this article preparePreviewImg,previewImgList,readImgFile,cmpMatrix,print2DMatrix, Please refer to 《https://blog.csdn.net/LaoYuanPython/article/details/111351901 OpenCV-Python Graphics and image processing : Some of the tools, functions, functions and calling syntax are introduced 》 In the introduction .

Four 、 The method of bleaching white paper photos

The above picture has been adjusted for contrast , But the background is not ideal white paper , The grayscale of the background color is also inconsistent . The following provides a better implementation of this image processing into a pure white background .

Code for bleaching background color :

import cv2
from opencvPublic import preparePreviewImg,previewImgList,readImgFile
def adjustWhitePaperBG():
img = readImgFile(r'f:\pic\paper.jpg', True)
kernel1 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
close = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel1)
div = np.float32(img) / (close)
res = np.uint8(cv2.normalize(div, div, 0, 255, cv2.NORM_MINMAX))
preparePreviewImg(' The input image : White paper photo ', img, fontSize=38, color=(255, 255, 255))
preparePreviewImg('https://blog.csdn.net/LaoYuanPython', None, fontSize=38, color=(255, 255, 255))
preparePreviewImg(' Output image : White paper ', res, fontSize=38, color=(255, 0, 0))
previewImgList()
print('\033[31m Old ape Python website :\033[34mhttps://blog.csdn.net/LaoYuanPython\033[0m, WeChat official account :\033[34m Old ape Python')
adjustWhitePaperBG()

Displayed input 、 Output image :
 Insert picture description here

5、 ... and 、 Summary

This section introduces a general method of forcibly adjusting image contrast using threshold and OpenCV-Python The implementation of the , At the same time, a case of background color bleaching based on white paper photos with text is introduced , Relevant methods can be used for image enhancement .

Reference material

https://stackoverflow.com/questions/10549245/how-can-i-adjust-contrast-in-opencv-in-c

For more information on image processing, please Reference column 《https://blog.csdn.net/laoyuanpython/category_9979286.html OpenCV-Python Graphics and image processing 》、《https://blog.csdn.net/laoyuanpython/category_10581071.html OpenCV-Python Difficult problem set for beginners 》 And 《 Basic knowledge of image processing 》 Introduction to .

If you have questions about the content of the article , You can leave a message in the comments section of the blog , Or follow the blog on the left : Old ape Python Wechat public number sends message for consultation .

It's not easy to write , Please support :

If you can get something from reading this article , Please give me some compliments 、 Comment on 、 Collection , Thank you for your support !

Pay column on the old ape

  1. Paid column 《https://blog.csdn.net/laoyuanpython/category_9607725.html Use PyQt Developing graphical interfaces Python application 》 Special introduction based on Python Of PyQt Basic course of graphical interface development , The corresponding article directory is 《 https://blog.csdn.net/LaoYuanPython/article/details/107580932 Use PyQt Developing graphical interfaces Python Application column Directory 》;
  2. Paid column 《https://blog.csdn.net/laoyuanpython/category_10232926.html moviepy Audio and video development column ) Detailed introduction moviepy The class correlation method of audio and video clip synthesis processing and the use of correlation method to process related clip synthesis scene , The corresponding article directory is 《https://blog.csdn.net/LaoYuanPython/article/details/107574583 moviepy Audio and video development column Directory 》;
  3. Paid column 《https://blog.csdn.net/laoyuanpython/category_10581071.html OpenCV-Python Difficult problem set for beginners 》 by 《https://blog.csdn.net/laoyuanpython/category_9979286.html OpenCV-Python Graphics and image processing 》 The accompanying column of , It is the author's opinion of OpenCV-Python Some problems encountered in the study of graphics and image processing , The relevant data are basically the results of repeated studies of old apes , be conducive to OpenCV-Python Beginners have a deeper understanding of OpenCV, The corresponding article directory is 《https://blog.csdn.net/LaoYuanPython/article/details/109713407 OpenCV-Python Catalog of difficult problem set for beginners
  4. Paid column 《https://blog.csdn.net/laoyuanpython/category_10762553.html Python Introduction to reptiles 》 From the perspective of Xiaobai, an Internet front-end developer, this paper introduces the knowledge and skills of crawler development , Including the basics of getting started with crawlers , And crawling CSDN The article information 、 Blogger information 、 Like the article 、 Comments, etc .

The first two columns are suitable for a certain amount of Python Xiaobai readers who are basic but have no relevant knowledge learn , In the third column, please combine 《https://blog.csdn.net/laoyuanpython/category_9979286.html OpenCV-Python Graphics and image processing 》 Learning to use .

For lack of Python Basic colleagues , Through the free column of the old ape 《https://blog.csdn.net/laoyuanpython/category_9831699.html special column :Python Basic course catalog ) Learn from scratch Python.

If you are interested and willing to support the old ape readers , Welcome to the paid column .

Old ape Python, Learn from the old ape Python!

* ░ Go to the old ape Python Blog Directory https://blog.csdn.net/LaoYuanPython

版权声明
本文为[LaoYuanPython]所创,转载请带上原文链接,感谢
https://pythonmana.com/2021/10/20211014090205889D.html

  1. Inverse un entier de 3 bits (implémentation Python)
  2. Compared with Excel, it is easy to learn Python report automation practice!
  3. 7 excellent open source libraries for learning Python Programming
  4. Python Qt GUI设计:QPrinter打印图片类(基础篇—21)
  5. Use of Python pandas!!!!! Explain in detail
  6. 2n行输入,Python,判断字母个数
  7. Notes de Python (XV): dérivation de liste
  8. Notes sur Python (XVI): générateur et Itérateur
  9. Notes de Python (18): décorateur
  10. 2n entrée de ligne, Python, nombre de lettres de jugement
  11. Notes Python (17): fermetures
  12. Notes sur Python (20): fonctions d'ordre supérieur intégrées
  13. 想问问这个Python编程咋做呀?
  14. 想問問這個Python編程咋做呀?
  15. Vous voulez savoir ce que fait cette programmation python?
  16. 11.5K Star,一个开源的 Python 静态类型检查库
  17. Sweetviz:让你只需三行代码实现Python探索性数据分析
  18. Sweetviz:讓你只需三行代碼實現Python探索性數據分析
  19. Sweetviz: vous permet d'effectuer une analyse exploratoire des données python avec seulement trois lignes de code
  20. 11.5k Star, une bibliothèque de vérification de type statique Python Open Source
  21. 刚学Python,想让大大给我解释一下代码
  22. 剛學Python,想讓大大給我解釋一下代碼
  23. Je viens d'apprendre Python et je veux que tu m'expliques le Code.
  24. Python QT GUI Design: qmainwindow, QWidget and qdialog window classes (Fundamentals - 10)
  25. Python爬虫项目实战:快手网页版滑块captchaSession分析
  26. python计算时间十二小时制
  27. Temps de calcul Python 12 heures
  28. Python crawler Project actual Fighting: faster Web Version Slider CAPTCHA session Analysis
  29. Python要学习多久可以掌握?多久可以精通?
  30. Combien de temps Python va - t - il apprendre à maîtriser? Combien de temps faut - il pour maîtriser?
  31. 从官网上下载的python安装包安装不了
  32. 深度学习项目:如何使用Python和OpenCV进行人脸识别
  33. python编辑语言如内容所示
  34. La langue d'édition Python est affichée dans le contenu
  35. 有谁知道这怎么回事嘛(Python的简单代码)
  36. 有誰知道這怎麼回事嘛(Python的簡單代碼)
  37. Qui sait ce qui se passe?
  38. Python求某个数的因数【因数是指能被这个数整除的数。例如6的因数有:1、2、3、6; 7的因数有:1、7; 8的因数有:1、2、4、8】。
  39. Python calcule les facteurs d'un nombre [les facteurs sont des nombres qui peuvent être divisés par ce nombre. Par exemple, les facteurs de 6 sont: 1, 2, 3, 6; les facteurs de 7 sont: 1, 7; et les facteurs de 8 sont: 1, 2, 4, 8].
  40. 请问这个实验如何做(用python3.x的环境)
  41. 如何创建一个python程序来模拟电影院的座位预订
  42. [algorithm learning] 1486. Array XOR operation (Java / C / C + + / Python / go / trust)
  43. Comment créer un programme Python pour simuler la réservation de sièges dans un cinéma
  44. Comment faire cette expérience (en utilisant l'environnement Python 3.x)
  45. Python之绘制多角星
  46. Pandas的SettingWithCopyWarning报警怎么解决
  47. Comment résoudre l'alarme de réglage avec copywarning de pandas
  48. 使用 Python 进行数据可视化之Matplotlib
  49. Python 常见问题 - 使用 poetry build 打包构建失败,报 ModuleOrPackageNotFound No file/folder found for package
  50. 万圣节奇妙夜,不给糖就捣蛋—Python带你点击解锁恐怖新道具(南瓜头、骷髅、蝙蝠、糖......)
  51. 【算法学习】1221. 分割平衡字符串(java / c / c++ / python / go / rust)
  52. 教你用Python 编写 Hadoop MapReduce 程序
  53. Je t'ai appris à écrire Hadoop mapreduce en Python.
  54. Pandas下载最新版和python不匹配
  55. python tkinter GUI编程如何实现一个时钟,无论主线程怎么卡住,都不影响时钟正常进行走时间。
  56. python tkinter GUI編程如何實現一個時鐘,無論主線程怎麼卡住,都不影響時鐘正常進行走時間。
  57. La programmation de l'interface graphique Python tkinter pour implémenter une horloge n'affecte pas le temps de fonctionnement normal de l'horloge, peu importe comment le fil principal est coincé.
  58. Pandas télécharger la dernière version ne correspond pas à Python
  59. Make a cool music player with Python [V3.0 including source code and packaged exe]
  60. 编写一个函数,可以传入两个实参或三个实参。如果传入两个实参,返回求和。如果传入三个实参,返回的话。前两个实参的和对第三个实参求余的结果Python