<<<写在前面>>>写在前面>
近期手上有一个电子显微镜,但是没有相应的电脑程序,只有系统自带的相机,正好曾经用过 OpenCV 做过些东西,于是重操旧业,由于设备自身摄像头并不很好,优化效果相比原生画面只有一点点提升。
安装环境
我是有用的是 conda 管理 python 版本的
先官网下载 conda 安装程序,双击打开安装完成后打开终端
1 2 3
| conda create -n opencv conda activate opencv conda install -n opencv ipykernel ipython
|
图像优化代码
图像优化代码
复制以下代码在 py 文件中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| import cv2 import numpy as np
cap = cv2.VideoCapture(0)
if not cap.isOpened(): print("无法打开摄像头") exit()
window_title = 'Enhanced Image' cv2.namedWindow(window_title, cv2.WINDOW_NORMAL | cv2.WINDOW_KEEPRATIO)
bilateral_filter_diameter = 7 color_space_std = 50 coordinate_space_std = 50 nl_means_denoising_strength = 8 nl_means_search_window_size = 8 nl_means_similar_window_size = 5 nl_means_color_channels = 15
mode = 2
def switch_mode(event, x, y, flags, param): global mode if event == cv2.EVENT_LBUTTONDOWN: if mode == 1: mode = 2 else: mode = 1
while True: ret, frame = cap.read()
if not ret: print("无法读取帧") break
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) average_brightness = np.mean(gray_frame)
if mode == 1: filtered_frame = cv2.bilateralFilter(frame, bilateral_filter_diameter, color_space_std, coordinate_space_std)
weight = 0.7 high_pass = cv2.subtract(frame, cv2.multiply(filtered_frame, weight))
enhancement_weight = 1.3 enhanced_frame = cv2.add(frame, cv2.multiply(high_pass, enhancement_weight))
denoised_frame = cv2.fastNlMeansDenoisingColored(enhanced_frame, None, nl_means_denoising_strength, nl_means_search_window_size, nl_means_similar_window_size, nl_means_color_channels)
if average_brightness > 150: filter_size = 7 else: filter_size = 5 final_frame = cv2.medianBlur(denoised_frame, filter_size) else: final_frame = frame
cv2.imshow(window_title, final_frame) cv2.resizeWindow(window_title, final_frame.shape[1], final_frame.shape[0]+60)
if cv2.waitKey(1) & 0xFF == ord('q'): break
cap.release() cv2.destroyAllWindows()
|
效果图对比
感觉两个大差不差的,可能是图源就不太好吧…
瞎拍拍
拍了三个显示屏显微镜下的图,猜猜哪个是 LCD 哪个是 OLED
<<<文末闲语>>>文末闲语>
代码一顿倒腾,结果感觉没有达到优化图像的效果,不过感觉动态画面噪点可能相对没那么明显吧,有了这个显微镜倒是方便看焊接是否完美,有没有连锡虚焊之类的。