图像处理在现代技术中扮演着重要的角色,广泛应用于计算机视觉、图像分析和机器学习等领域。本文将介绍一种简单的图像处理方法,主要包括灰度转换、去除边框、提取有效区域和图像分割,并提供相应的 Swift 代码示例。
灰度转换
灰度转换是将彩色图像转换为灰度图像的技术,目的是减少图像的复杂性。在 Swift 中,我们可以使用 Core Graphics 来实现灰度转换:
swift
import UIKit
func convertToGray(image: UIImage) -> UIImage? {
let size = image.size
UIGraphicsBeginImageContext(size)
let context = UIGraphicsGetCurrentContext()
context?.translateBy(x: 0, y: size.height)
context?.scaleBy(x: 1.0, y: -1.0)
context?.setBlendMode(.copy)
context?.draw(image.cgImage!, in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
context?.setBlendMode(.sourceIn)
context?.setFillColor(UIColor.gray.cgColor)
context?.fill(CGRect(x: 0, y: 0, width: size.width, height: size.height))
let grayImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return grayImage
}
去除图像边框
去除图像的边框可以通过遍历图像的每一行和每一列来实现。以下是相应的代码:
swift
func clearBorders(image: UIImage, borderWidth: Int) -> UIImage? {
guard let cgImage = image.cgImage else { return nil }
let width = cgImage.width
let height = cgImage.height
let colorSpace = CGColorSpaceCreateDeviceRGB()
guard let context = CGContext(data: nil, width: width, height: height, bitsPerComponent: 8, bytesPerRow: 0, space: colorSpace, bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue) else { return nil }
context.draw(cgImage, in: CGRect(x: 0, y: 0, width: width, height: height))
let data = context.data?.assumingMemoryBound(to: UInt8.self)
for y in 0..<height {
for x in 0..<width {
if x < borderWidth || y < borderWidth || x >= width - borderWidth || y >= height - borderWidth {
let pixelIndex = (y * width + x) * 4
data?[pixelIndex] = 255 // Red
data?[pixelIndex + 1] = 255 // Green
data?[pixelIndex + 2] = 255 // Blue
}
}
}
let newCGImage = context.makeImage()
return newCGImage != nil ? UIImage(cgImage: newCGImage!) : nil
}
提取有效区域
有效区域提取是图像分析中的关键步骤。我们可以使用以下代码来提取有效区域:
swift
func extractValidRegion(image: UIImage, grayThreshold: UInt8) -> UIImage? {
guard let cgImage = image.cgImage else { return nil }
let width = cgImage.width
let height = cgImage.height
let colorSpace = CGColorSpaceCreateDeviceRGB()
guard let context = CGContext(data: nil, width: width, height: height, bitsPerComponent: 8, bytesPerRow: 0, space: colorSpace, bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue) else { return nil }
context.draw(cgImage, in: CGRect(x: 0, y: 0, width: width, height: height))
let data = context.data?.assumingMemoryBound(to: UInt8.self)
var minX = width, minY = height
var maxX = 0, maxY = 0
for y in 0..<height {
for x in 0..<width {
let pixelIndex = (y * width + x) * 4
let grayValue = data?[pixelIndex] ?? 255
if grayValue < grayThreshold {
if minX > x { minX = x }
if minY > y { minY = y }
if maxX < x { maxX = x }
if maxY < y { maxY = y }
}
}
}
let validRegion = context.makeImage()?.cropping(to: CGRect(x: minX, y: minY, width: maxX - minX + 1, height: maxY - minY + 1))
return validRegion != nil ? UIImage(cgImage: validRegion!) : nil
}更多内容联系1436423940
图像分割
图像分割可以将图像划分为多个小块。以下是实现这一功能的代码:
swift
func splitImage(image: UIImage, rows: Int, cols: Int) -> [UIImage] {
guard let cgImage = image.cgImage else { return [] }
let width = cgImage.width
let height = cgImage.height
var splitImages: [UIImage] = []
let pieceWidth = width / cols
let pieceHeight = height / rows
for i in 0..<rows {
for j in 0..<cols {
let rect = CGRect(x: j * pieceWidth, y: i * pieceHeight, width: pieceWidth, height: pieceHeight)
if let pieceCGImage = cgImage.cropping(to: rect) {
splitImages.append(UIImage(cgImage: pieceCGImage))
}
}
}
return splitImages
}
生成二进制编码
最后,将灰度图像转换为二进制字符串:
swift
func generateBinaryCode(image: UIImage, grayThreshold: UInt8) -> String {
guard let cgImage = image.cgImage else { return "" }
let width = cgImage.width
let height = cgImage.height
let colorSpace = CGColorSpaceCreateDeviceRGB()
guard let context = CGContext(data: nil, width: width, height: height, bitsPerComponent: 8, bytesPerRow: 0, space: colorSpace, bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue) else { return "" }
context.draw(cgImage, in: CGRect(x: 0, y: 0, width: width, height: height))
let data = context.data?.assumingMemoryBound(to: UInt8.self)
var binaryCode = ""
for y in 0..<height {
for x in 0..<width {
let pixelIndex = (y * width + x) * 4
let grayValue = data?[pixelIndex] ?? 255
binaryCode.append(grayValue < grayThreshold ? "1" : "0")
}
}
return binaryCode
}
标签:cgImage,context,height,width,图像处理,let,应用,UIImage,Swift From: https://www.cnblogs.com/ocr1/p/18500031