前提
以下のサイトの、黒い部分(サイトは暗い部分)の面積の割合を求めようとしています。
https://rightcode.co.jp/blog/information-technology/python-image-analysis-hsv-rust-area
発生している問題・エラーメッセージ
suteteok.jpegが今回試している画像です。
suteteok.jpeg(image/jpeg) - 83224 bytes, last modified: 2023/1/22 - 100% done Saving suteteok.jpeg to suteteok.jpeg --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-17-5c6a3d6b2d48> in <module> 6 7 img_bw =cv2.imread('uploaded_file', 2) ----> 8 hit, wid = img_bw.shape 9 10 # 一定の敷居値(今回は120)より暗い箇所のみ表示 ## AttributeError: 'NoneType' object has no attribute 'shape'
どこを変更すれば、shapeがcolaboratory上で読み込めるようになるでしょうか。
該当のソースコード
python
1import cv2 2import numpy as np 3import matplotlib.pyplot as pyplot 4from google.colab import files 5uploaded_file = files.upload() 6 7img_bw =cv2.imread('uploaded_file', 2) 8hit, wid = img_bw.shape 9 10# 一定の敷居値(今回は120)より暗い箇所のみ表示 ## 11from numpy import sum 12img_bw_m = np.where(img_bw < 120, 1, 0) 13pyplot.imshow(img_bw_m) 14print("Rust ratio is :", sum(img_bw_m) / (hit * wid) * 100, '%')
試したこと
anacondaのspiderを使ってデスクトップ上で画像を直接読むように、書き換えるとうまくいきました。
python
1import cv2 2import numpy as np 3import matplotlib.pyplot as pyplot 4 5img_bw =cv2.imread('suteteok.jpeg', 2) 6hit, wid = img_bw.shape 7 8# 一定の敷居値(今回は120)より暗い箇所のみ表示 ## 9from numpy import sum 10img_bw_m = np.where(img_bw < 120, 1, 0) 11pyplot.imshow(img_bw_m) 12print("Rust ratio is :", sum(img_bw_m) / (hit * wid) * 100, '%')
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。