数字图像处理的图像操作

数字图像处理的图像操作

    正在检查是否收录...

图像操作在计算机视觉和图像处理中发挥着至关重要的作用。这些操作对于诸如预处理、增强图像质量和启用高级算法等任务至关重要。在计算机视觉中,诸如调整大小、裁剪、调整亮度/对比度/伽玛和几何变换等操作是基础的。它们允许进行高效的计算、提取感兴趣区域、规范化图像强度和几何校准。同样,在图像处理中,这些操作对于降采样、裁剪不需要的区域、增强可见性和质量以及执行几何操作都至关重要。

调整大小

在各种场景中,调整图像大小是常见的,可以实现不同的目的,例如将图像适应特定尺寸或减小文件大小。图像插值和重采样是图像处理和计算机视觉中用于调整图像大小或比例的技术。

图像插值

图像插值是指根据已知像素值在图像内未知位置上估算像素值的过程。不同的插值方法使用不同的方式来估算未知像素的值。

最近邻插值将未知像素位置的值分配为最近的已知像素值。这种方法简单但可能导致出现块状伪影和丢失细节。

最近邻插值

双线性插值考虑了四个最近的已知像素的值,并计算加权平均来估算未知像素的值。与最近邻插值相比,它产生更平滑的结果,但仍可能引入一些模糊。

双三次插值通过考虑更多的相邻像素并使用三次多项式来估算像素值,扩展了双线性插值。这种方法可以提供更高质量的结果,具有更平滑的过渡和更好的保留图像细节。

import cv2 import numpy as np def resize_image(image, scale, interpolation): width = int(image.shape[1] * scale) height = int(image.shape[0] * scale) resized_image = cv2.resize(image, (width, height), interpolation=interpolation) return resized_image SCALE = 4 # Load the image image_path = "image.png" image = cv2.imread(image_path) # Resize the image using nearest neighbor interpolation nearest_neighbor_resized = resize_image(image, scale=SCALE, interpolation=cv2.INTER_NEAREST) # Resize the image using bilinear interpolation bilinear_resized = resize_image(image, scale=SCALE, interpolation=cv2.INTER_LINEAR) # Resize the image using bicubic interpolation bicubic_resized = resize_image(image, scale=SCALE, interpolation=cv2.INTER_CUBIC)

裁剪

裁剪图像的目的是去除不需要的内容或聚焦于特定的感兴趣区域。裁剪使您能够优化构图,消除干扰,并突出图像中的重要元素。去除不必要或无关的部分可以创造出视觉上吸引人且具有影响力的图像,有效地传达预期的信息或主题。

可以使用不同的方法来确定裁剪区域:

  • 手动选择:手动裁剪涉及对图像进行视觉检查并选择要保留的所需区域。这种方法提供了灵活性,并允许基于摄影师或设计师的艺术判断做主观决定。
  • 目标检测:基于目标检测算法的自动裁剪技术可以识别并提取图像中的特定对象或主题。这些算法分析图像并根据预定义的模式或经过训练的模型定位对象。检测到的对象可以作为裁剪区域,确保保留重要元素同时去除无关的背景或周围区域。
  • 分割:可以使用图像分割技术,如语义分割或实例分割,将图像分成有意义的区域。这些技术为不同的对象或区域分配标签或掩码,使得可以裁剪特定的部分或隔离感兴趣的特定区域。

import cv2 def crop_image(image, x, y, width, height): cropped_image = image[y:y+height, x:x+width] return cropped_image # Example usage image = cv2.imread("cath.jpeg") cropped_image = crop_image(image, x=400, y=500, width=300, height=200) cv2.imshow("Cropped Image", cropped_image) cv2.waitKey(0) cv2.destroyAllWindows()

调整

亮度和对比度:

调整亮度和对比度对于增强图像的可见性和提高视觉吸引力至关重要。调整亮度可以使图像看起来更明亮或更暗,突显曝光不足或曝光过度的区域的细节。对比度调整增强了光亮和阴暗区域之间的区别,使图像显得更清晰和更动态。

通过控制亮度和对比度,您可以提高图像的整体质量和可读性,确保重要的特征能够清晰可辨。

import cv2 import numpy as np image_path = "cath.jpeg" def adjust_brightness(image, value): # Convert the image to the HSV color space hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) # Split the channels h, s, v = cv2.split(hsv) # Apply the brightness adjustment v = cv2.add(v, value) # Clamp the values to the valid range of 0-255 v = np.clip(v, 0, 255) # Merge the channels back together hsv = cv2.merge((h, s, v)) # Convert the image back to the BGR color space adjusted_image = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR) return adjusted_image def adjust_contrast(image, value): # Convert the image to the LAB color space lab = cv2.cvtColor(image, cv2.COLOR_BGR2LAB) # Split the channels l, a, b = cv2.split(lab) # Apply the contrast adjustment l = cv2.multiply(l, value) # Clamp the values to the valid range of 0-255 l = np.clip(l, 0, 255) # Merge the channels back together lab = cv2.merge((l, a, b)) # Convert the image back to the BGR color space adjusted_image = cv2.cvtColor(lab, cv2.COLOR_LAB2BGR) return adjusted_image # Load the image image = cv2.imread(image_path) # Adjust the brightness brightness_adjusted = adjust_brightness(image, value=50) # Adjust the contrast contrast_adjusted = adjust_contrast(image, value=2) # Display the original and adjusted images cv2.imshow("Original", image) cv2.imshow("Brightness Adjusted", brightness_adjusted) cv2.imshow("Contrast Adjusted", contrast_adjusted) cv2.waitKey(0) cv2.destroyAllWindows()

直方图均衡化

直方图均衡化是一种用于增强对比度的技术。它通过重新分配像素强度值以涵盖更广范围的值来实现这一目标。其主要目标是通过图像获得像素强度的更均匀分布。

通过重新分配像素强度,直方图均衡化增强了图像的对比度。

 import cv2 import matplotlib.pyplot as plt image_path = "cath.jpeg" image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE) # Apply histogram equalization equalized_image = cv2.equalizeHist(image) # Calculate histograms hist_original = cv2.calcHist([image], [0], None, [256], [0, 256]) hist_equalized = cv2.calcHist([equalized_image], [0], None, [256], [0, 256]) # Plot the histograms plt.figure(figsize=(10, 5)) plt.subplot(1, 2, 1) plt.plot(hist_original, color='b') plt.title("Original Image Histogram") plt.xlabel("Pixel Intensity") plt.ylabel("Frequency") plt.subplot(1, 2, 2) plt.plot(hist_equalized, color='r') plt.title("Equalized Image Histogram") plt.xlabel("Pixel Intensity") plt.ylabel("Frequency") plt.tight_layout() plt.show()

直方图

# Display the original and equalized images fig, axes = plt.subplots(1, 2, figsize=(10, 5)) axes[0].imshow(image, cmap='gray') axes[0].set_title("Original") axes[0].axis("off") axes[1].imshow(equalized_image, cmap='gray') axes[1].set_title("Equalized") axes[1].axis("off") plt.tight_layout() plt.show()

均衡化图像

线性缩放

线性缩放,也称为对比度拉伸,用于通过线性映射原始像素值到一个新范围来调整图像的亮度和对比度。该过程涉及根据图像中的最小值和最大值重新缩放像素值,以利用完整的动态范围。

线性缩放允许对亮度和对比度的调整进行精确控制。您可以根据特定要求定义所需的强度范围。

import cv2 import numpy as np import matplotlib.pyplot as plt # Load the image image_path = "cath.jpeg" image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE) # Calculate the minimum and maximum pixel values in the image min_value = np.min(image) max_value = np.max(image) # Define the desired minimum and maximum intensity values for the output image new_min = 5 new_max = 10 # Perform linear scaling scaled_image = cv2.convertScaleAbs(image, alpha=(new_max - new_min) / (max_value - min_value), beta=new_min - min_value * (new_max - new_min) / (max_value - min_value)) # Display the original and scaled images fig, axes = plt.subplots(1, 2, figsize=(10, 5)) axes[0].imshow(cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)) axes[0].set_title("Original") axes[0].axis("off") axes[1].imshow(scaled_image, cmap='gray') axes[1].set_title("Scaled") axes[1].axis("off") plt.tight_layout() plt.show()

线性缩放

伽马校正

伽马校正是一种用于纠正图像输入像素值与显示输出强度之间的非线性强度关系的技术。它考虑到人类视觉系统对光的非线性响应,并旨在实现更准确和感知一致的图像表示。

相机捕捉或存储在图像文件中的像素值与人类感知亮度之间的关系是非线性的。换句话说,像素值的线性增加并不导致感知亮度的线性增加。这种非线性关系是由于成像传感器和人类视觉系统的响应特性导致的。

伽马校正基于一个称为伽马(γ)的参数。伽马值表示输入像素值和显示输出强度之间的关系。它是两者之间非线性映射的度量。

伽马校正对像素值应用幂律变换,调整强度值以校正非线性响应。伽马校正的公式如下:

校正值 = 输入值 ^ (1 / 伽马)

这里,输入值代表原始像素值,校正值代表调整后的像素值。

伽马校正的主要作用是补偿非线性强度关系,确保图像中的颜色和细节得到准确的表示。伽马校正发挥重要作用的方式如下:

  • 亮度补偿:伽马校正有助于弥补捕捉和显示设备之间亮度响应的差异。它确保显示图像中的感知亮度水平与原始场景一致。
  • 对比度增强:伽马校正可以通过重新分配色调值来增强图像的对比度。根据伽马值的不同,它可以有效地强调图像的暗区域或亮区域中的细节。
  • 色彩准确性:伽马校正有助于实现准确的颜色表示。通过调整伽马值,可以改善颜色再现,确保颜色看起来更自然且忠实于原始场景。
  • 色调映射:在高动态范围(HDR)成像中,伽马校正常常作为色调映射技术的一部分,将场景的广泛动态范围映射到显示设备的有限动态范围。伽马校正有助于保持阴影和高光区域的细节,防止信息丢失。
  • 感知一致性:伽马校正旨在实现感知上一致的图像,其中显示的强度与人类视觉感知一致。通过校正非线性响应,伽马校正确保图像对观众呈现出视觉上愉悦和逼真的效果。
import cv2 import numpy as np image_path = "cath.jpeg" def adjust_gamma(image, gamma): # Build a lookup table mapping the input pixel values to the corrected gamma values lookup_table = np.array([((i / 255.0) ** gamma) * 255 for i in np.arange(0, 256)]).astype(np.uint8) # Apply gamma correction using the lookup table gamma_corrected = cv2.LUT(image, lookup_table) return gamma_corrected # Load the image image = cv2.imread(image_path) # Adjust the gamma value gamma_value = 1.5 gamma_corrected = adjust_gamma(image, gamma_value) # Display the original and gamma-corrected images cv2.imshow("Original", image) cv2.imshow("Gamma Corrected", gamma_corrected) cv2.waitKey(0) cv2.destroyAllWindows()

伽马校正

几何变换

几何变换使图像的透视、方向和空间关系发生变化。这些变换为图像对齐、目标检测、图像注册等任务提供了基本工具。

(1) 平移

平移是一种基本的几何变换,涉及将图像水平或垂直移动指定的距离。

import cv2 import numpy as np image_path = "cath.jpeg" image = cv2.imread(image_path) # Define the translation matrix tx = 100 # pixels to shift in the x-axis ty = 50 # pixels to shift in the y-axis translation_matrix = np.float32([[1, 0, tx], [0, 1, ty]]) # Apply translation translated_image = cv2.warpAffine(image, translation_matrix, (image.shape[1], image.shape[0])) # Display the original and translated images cv2.imshow("Original", image) cv2.imshow("Translated", translated_image) cv2.waitKey(0) cv2.destroyAllWindows()

平移

(2) 缩放

缩放是指调整图像的大小,可以通过对所有维度应用统一的缩放因子,或者使用不同的缩放因子来调整不同的维度。已缩放。

# Define the scaling factors scale_x = 1.5 # scaling factor for the x-axis scale_y = 0.8 # scaling factor for the y-axis # Apply scaling scaled_image = cv2.resize(image, None, fx=scale_x, fy=scale_y, interpolation=cv2.INTER_LINEAR) # Display the original and scaled images cv2.imshow("Original", image) cv2.imshow("Scaled", scaled_image) cv2.waitKey(0) cv2.destroyAllWindows()

缩放

(3) 旋转

旋转是一种几何变换,涉及围绕中心点按指定角度更改图像的方向。

# Define the rotation angle angle = 30 # Perform rotation rows, cols = image.shape[:2] rotation_matrix = cv2.getRotationMatrix2D((cols / 2, rows / 2), angle, 1) rotated_image = cv2.warpAffine(image, rotation_matrix, (cols, rows)) # Display the original and rotated images cv2.imshow("Original", image) cv2.imshow("Rotated", rotated_image) cv2.waitKey(0) cv2.destroyAllWindows()

旋转

codeammerpappwindowsnumpyimudefi调整亮度图像处理目标检测cto计算机视觉可见性clirpaxlamvpclip可读性b2burl准确性吸引力jaxdns图像操作高质量视觉吸引力语义分割tpu设计师hdr图像分割图像输入摄影师实例分割图像质量加权平均视觉感知一致性ai作图工具
  • 本文作者:李琛
  • 本文链接: https://wapzz.net/post-3342.html
  • 版权声明:本博客所有文章除特别声明外,均默认采用 CC BY-NC-SA 4.0 许可协议。
本站部分内容来源于网络转载,仅供学习交流使用。如涉及版权问题,请及时联系我们,我们将第一时间处理。
文章很赞!支持一下吧 还没有人为TA充电
为TA充电
还没有人为TA充电
0
  • 支付宝打赏
    支付宝扫一扫
  • 微信打赏
    微信扫一扫
感谢支持
文章很赞!支持一下吧
关于作者
2.3W+
5
0
1
WAP站长官方

微星明天将发布尊爵系列AI笔记本 首批搭载酷睿Ultra处理器

上一篇

ComfyUI部署流程

下一篇
  • 复制图片
按住ctrl可打开默认菜单