影像處理在現代技術中扮演著重要的角色,廣泛應用於計算機視覺、影像分析和機器學習等領域。本文將介紹一種簡單的影像處理方法,主要包括灰度轉換、去除邊框、提取有效區域和影像分割,並提供相應的 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
}