[Swift + OpenCV] signal SIGABRTエラー
解決したいこと
OpenCVを用いて輪郭抽出のコードを作成しています.
下記のコードを作成し実行したところ,シミュレーターは起動するのですが,ボタン(change img)をタップしたところ,エラーが発生しました.
解決方法を教えてください.
発生している問題・エラー
Thread 1: signal SIGABRT
該当するソースコード
import SwiftUI
import opencv2
struct ContentView: View {
@State var img:UIImage? = nil
var body: some View {
if let img = img {
Image(uiImage: img)
.resizable()
.scaledToFit()
}
Button(action: {
let readImg = UIImage(named: "001")
let src = Mat(uiImage: readImg!)
// グレースケール
let dstGray = Mat()
Imgproc.cvtColor(src: src, dst: dstGray, code: ColorConversionCodes.COLOR_RGB2GRAY)
// 平滑化
let dstBlur = Mat()
Imgproc.blur(src: dstGray, dst: dstBlur, ksize: Size2i(width: 9, height: 9))
// 二値化
let dstThresh = Mat()
Imgproc.threshold(src: dstBlur, dst: dstThresh, thresh: 100, maxval: 255, type: ThresholdTypes.THRESH_OTSU)
//エッジ検出
let dstCanny = Mat()
Imgproc.Canny(image: dstThresh, edges: dstCanny, threshold1: 10, threshold2: 360)
// 輪郭の取得
let dstLine = Mat()
let contours: NSMutableArray = []
Imgproc.findContours(image: dstCanny, contours: contours, hierarchy: dstLine, mode: RetrievalModes.RETR_EXTERNAL, method: ContourApproximationModes.CHAIN_APPROX_SIMPLE)
// 型のキャスト
let contoursp2i = contours as! [[Point2i]]
// 輪郭の描画
for _ in 0..<contours.count {
let area = Imgproc.contourArea(contour: dstLine)
if area > 1000 {
Imgproc.drawContours(image: src, contours: contoursp2i, contourIdx: -1, color: Scalar(255, 0, 0, 255), thickness: 20)
}
}
img = src.toUIImage()
print (contours.count)
}){
Text("change img")
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
自分で試したこと
クリーンして再ビルドなど,基本的なことは試しました.
また,エラー文を検索しましたが,解決に至りませんでした.
環境
macOS 14.1.2
Xcode 15.0.1
1 likes