質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
OpenCV

OpenCV(オープンソースコンピュータービジョン)は、1999年にインテルが開発・公開したオープンソースのコンピュータビジョン向けのクロスプラットフォームライブラリです。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

0回答

1021閲覧

RuntimeWarning: invalid value encountered in double_scalars

Gencoflu61

総合スコア1

OpenCV

OpenCV(オープンソースコンピュータービジョン)は、1999年にインテルが開発・公開したオープンソースのコンピュータビジョン向けのクロスプラットフォームライブラリです。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2021/12/06 13:13

このコードでの私の目的は、カメラを使用して取得した画像内の黒いオブジェクトの距離を測定することですが、以下のエラーで述べたように、このイベントは発生しません。
コードは次のとおりです。

import cv2 import numpy as np def make_coordinates(image, line_parameters): slope, intercept = line_parameters y1 = image.shape[0] y2 = int(y1*(3/5)) x1 = int((y1 - intercept)/slope) x2 = int((y2 - intercept)/slope) return np.array([x1, y1, x2, y2]) def average_slope_intercept(image, lines): left_fit = [] right_fit = [] if lines is not None: for line in lines: x1, y1, x2, y2 = line.reshape(4) parameters = np.polyfit((x1, x2), (y1, y2), 1) slope = parameters[0] intercept = parameters[1] if slope < 0: left_fit.append((slope, intercept)) else: right_fit.append((slope, intercept)) left_fit_average = np.average(left_fit, axis=0) #right_fit_average = np.average(right_fit, axis=0) print(left_fit_average, 'left') #print(right_fit_average, 'right') left_line = make_coordinates(image, left_fit_average) #right_line = make_coordinates(image, right_fit_average) return np.array([left_line]) def distance_to_camera(knownWidth, focalLength, perWidth): return (knownWidth * focalLength) / perWidth # The first step in finding the distance from the object or mark in the image is to calibrate and calculate the focal length . So , We need to know : # Initialize the distance from a known object to the camera KNOWN_DISTANCE = 24.0 # Initializes the width of a known object KNOWN_WIDTH = 11.0 def canny(img): hsv = cv2.cvtColor(img, cv2.COLOR_RBG2HSV) black_lower = np.array([0, 0, 0]) black_upper = np.array([180, 255, 30]) black_mask = cv2.inRange(hsv, black_lower, black_upper) kernel = 5 blur = cv2.GaussianBlur(black_mask, (kernel, kernel),0) canny = cv2.Canny(blur, 50, 150) return canny def display_lines(img, lines): line_image = np.zeros_like(img) if lines is not None: for line in lines: for x1, y1, x2, y2 in line: cv2.line(line_image, (x1, y1), (x2, y2), (255, 0, 0), 10) return line_image def region_of_interest(canny): height = canny.shape[0] width = canny.shape[1] mask = np.zeros_like(canny) square = np.array([[(635,478),(0,478),(281,200)] ]) cv2.fillPoly(mask, square, 255) masked_image = cv2.bitwise_and(canny, mask) return masked_image cap = cv2.VideoCapture(0) _,c_image = cap.read() marker = canny(c_image) focalLength = (marker [1][0] * KNOWN_DISTANCE) / KNOWN_WIDTH print('focalLength', focalLength) while True: _, frame = cap.read() canny_image = canny(frame) cropped_canny = region_of_interest(canny_image) lines = cv2.HoughLinesP(cropped_canny, 2, np.pi / 180, 100, np.array([]), minLineLength=40, maxLineGap=5) averaged_lines = average_slope_intercept(frame, lines) line_image = display_lines(frame, averaged_lines) combo_image = cv2.addWeighted(frame, 0.8, line_image, 1, 1) CM = distance_to_camera(KNOWN_WIDTH, focalLength, marker[1][0]) cv2.imshow("result", combo_image) cv2.putText(frame, "%.2fft" % CM, (frame.shape[1] - 200, frame.shape[0] - 20), cv2.FONT_HERSHEY_SIMPLEX, 2.0, (0, 255, 0), 3) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows()

エラーは下の写真のようです。

>>> %Run mesafe.py focalLength 0.0 [-3.89346104e-01 4.41339935e+02] left mesafe.py:18: RuntimeWarning: invalid value encountered in double_scalars return (knownWidth * focalLength) / perWidth [-3.90163497e-01 4.41415626e+02] left [-3.85150497e-01 4.40625233e+02] left [-3.93702170e-01 4.43145302e+02] left

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問