ヘディングのテキスト### 実現したいこと
swiftを用いたiOS向け画像二値化アプリの開発
前提
swiftでiOS向け画像二値化アプリの開発をおこなっています。
下のサイト様を参考に開発を進めていたのですが、アンラップがうまくいきません。お力添えを頂けますと幸いです。ttp://harumi.sakura.ne.jp/wordpress/2019/06/03/%E4%BA%8C%E5%80%A4%E5%8C%96/
発生している問題・エラーメッセージ
下記のプログラム中の154行目else文、"アンラップ失敗"は本来望んでいない状況なのですが、こちらが表示されてしまいます。 150行目のif文に入るためにはどこを直すべきでしょうか?
// // ContentView.swift // Image_Swift // // import SwiftUI import UIKit extension UIImage { func createBinarizedImage(r:[CGFloat], g: [CGFloat], b:[CGFloat], a:[CGFloat], threshold:CGFloat) -> UIImage{ UIGraphicsBeginImageContextWithOptions(size, false, 0) let wid:Int = Int(size.width) let hei:Int = Int(size.height) //let threshold:CGFloat = 128/255 for w in 0..<wid { for h in 0..<hei { let index = (w * wid) + h var color = 0.2126 * r[index] + 0.7152 * g[index] + 0.0722 * b[index] if color > threshold { color = 255 } else { color = 0 } UIColor(red: color, green: color, blue: color, alpha: a[index]).setFill() let drawRect = CGRect(x: w, y: h, width: 1, height: 1) UIRectFill(drawRect) draw(in: drawRect, blendMode: .destinationIn, alpha: 1) } } let binarizeImage = UIGraphicsGetImageFromCurrentImageContext()! UIGraphicsEndImageContext() return binarizeImage } } class ViewController: UIViewController { @IBOutlet weak var image: UIImageView! var r:[CGFloat] = [] var g:[CGFloat] = [] var b:[CGFloat] = [] var a:[CGFloat] = [] var threshold:CGFloat = 0.0 override func viewDidLoad() { super.viewDidLoad() let img = UIImage(named: "Lena")! // ピクセルごとの色情報の取得、下記のPixelBufferクラスの関数を使用している。 if let pixelBuffer = PixelBuffer(uiImage: img) { for x in 0..<pixelBuffer.width { for y in 0..<pixelBuffer.height { r.append(pixelBuffer.getRed(x: x, y: y)) g.append(pixelBuffer.getBlue(x: x, y: y)) b.append(pixelBuffer.getGreen(x: x, y: y)) a.append(pixelBuffer.getAlpha(x: x, y: y)) } } } else { print("image not format") } image.image = img.createBinarizedImage(r: r, g: g, b: b, a: a, threshold: threshold) } } class PixelBuffer { private var pixelData: Data var width: Int var height: Int private var bytesPerRow: Int private let bytesPerPixel = 4 //1ピクセルが4バイトのデータしか扱わない init?(uiImage: UIImage) { guard let cgImage = uiImage.cgImage, //R,G,B,A各8Bit cgImage.bitsPerComponent == 8, //1 pixelが32bit cgImage.bitsPerPixel == bytesPerPixel * 8 else { return nil } pixelData = cgImage.dataProvider!.data! as Data width = cgImage.width height = cgImage.height bytesPerRow = cgImage.bytesPerRow } func getRed(x: Int, y: Int) -> CGFloat { let pixelInfo = bytesPerRow * y + x * bytesPerPixel let r = CGFloat(pixelData[pixelInfo]) / CGFloat(255.0) return r } func getGreen(x: Int, y: Int) -> CGFloat { let pixelInfo = bytesPerRow * y + x * bytesPerPixel let green = CGFloat(pixelData[pixelInfo+1]) / CGFloat(255.0) return green } func getBlue(x: Int, y: Int) -> CGFloat { let pixelInfo = bytesPerRow * y + x * bytesPerPixel let blue = CGFloat(pixelData[pixelInfo+2]) / CGFloat(255.0) return blue } func getAlpha(x: Int, y: Int) -> CGFloat { let pixelInfo = bytesPerRow * y + x * bytesPerPixel let alpha = CGFloat(pixelData[pixelInfo+3]) / CGFloat(255.0) return alpha } } struct ContentView: View { @State var bin = 50.0 @State var isEditing = false @State var img = UIImage(named: "Lena") @State var img_bin :UIImage? init() { if let pixelBuffer = PixelBuffer(uiImage: img!) { var r = [CGFloat]() var g = [CGFloat]() var b = [CGFloat]() var a = [CGFloat]() for x in 0..<pixelBuffer.width { for y in 0..<pixelBuffer.height { r.append(pixelBuffer.getRed(x: x, y: y)) g.append(pixelBuffer.getGreen(x: x, y: y)) b.append(pixelBuffer.getBlue(x: x, y: y)) a.append(pixelBuffer.getAlpha(x: x, y: y)) } } img_bin = img!.createBinarizedImage(r: r, g: g, b: b, a: a, threshold:bin) } else { print("image not format") } } var body: some View { VStack{ Image(uiImage:img!) if let unwrap_img_bin = img_bin { Image(uiImage:unwrap_img_bin) } else{ Text("アンラップ失敗")//こっちになってしまっている } Slider( value: $bin, in: 0...100, onEditingChanged: { editing in isEditing = editing } ) Text("\(bin)") .foregroundColor(isEditing ? .red : .blue) } } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } }
試したこと
Lena以外の画像もいくつか試してみましたが、状況は変わりませんでした。
(最初回答のコメントに書いてしまったのですが、修正依頼のコメントにさせていただきました)
ご返信ありがとうございます。
ごめんなさい、見当違いのことを書いてしまったみたいですね。
nilのためアンラップした時のコードが動かないとのことですが、
`print("image not format")`のようなコードが
62行目と142行目にあると思います。
どちらかのメッセージがコンソールに出力されていると思うのですが、
どちらのメッセージが出力されているかもご記載いただけますでしょうか?
それから、可能でしたら、Lenaの画像か、他のでも良いですので、
「私が調べたところでは問題ない」画像をどこかアップしていただけますでしょうか?
ネット上にある既存の画像へのリンクでもOKです。

回答1件
あなたの回答
tips
プレビュー