0

Ich versuche Scikit-Image's adaptive Schwelle auf meinem Bild zu verwenden. I getestet, um ihren Beispielcode aus HERESkimage adaptive Thresholding auf ein Bild verwenden und die Ausgabe

import matplotlib.pyplot as plt 

from skimage import data 
from skimage.filters import threshold_otsu, threshold_adaptive 


image = data.page() 

global_thresh = threshold_otsu(image) 
binary_global = image > global_thresh 

block_size = 35 
binary_adaptive = threshold_adaptive(image, block_size, offset=10) 

fig, axes = plt.subplots(nrows=3, figsize=(7, 8)) 
ax0, ax1, ax2 = axes 
plt.gray() 

ax0.imshow(image) 
ax0.set_title('Image') 

ax1.imshow(binary_global) 
ax1.set_title('Global thresholding') 

ax2.imshow(binary_adaptive) 
ax2.set_title('Adaptive thresholding') 

for ax in axes: 
    ax.axis('off') 

plt.show() 

Der Code nimmt in einem Probenbild, Schwellen es und zeigt es PLT verwenden. Ich versuche jedoch, das numpige Array des Schwellenwertbildes zu erhalten. Wenn ich versuchte, cv2.imwrite für die Variable binary_global zu verwenden, funktioniert es nicht. Beim Drucken binary_global - es ist eigentlich ein Array, bestehend aus False und True Werte anstelle von Zahlen. Ich bin mir nicht sicher, wie plt das verwenden und ein Bild erzeugen kann. Unabhängig davon, wie kann ich das Bild schwellen und das Array des neuen Schwellenwertbilds mit den RGB-Werten abrufen?

Antwort

0

Sie müssen zuerst das Scikit-Image in opencv konvertieren, um cv2.imwrite() verwenden zu können.

Fügen Sie den folgenden Änderungen-

from skimage import img_as_ubyte 
import matplotlib.pyplot as plt 
from skimage import data 
from skimage.filters import threshold_otsu, threshold_adaptive 
import cv2 


image = data.page() 

global_thresh = threshold_otsu(image) 
binary_global = image > global_thresh 

block_size = 35 
binary_adaptive = threshold_adaptive(image, block_size, offset=10) 

fig, axes = plt.subplots(nrows=3, figsize=(7, 8)) 
ax0, ax1, ax2 = axes 
plt.gray() 

ax0.imshow(image) 
ax0.set_title('Image') 

ax1.imshow(binary_global) 
ax1.set_title('Global thresholding') 

ax2.imshow(binary_adaptive) 
ax2.set_title('Adaptive thresholding') 

for ax in axes: 
    ax.axis('off') 

plt.show() 
img = img_as_ubyte(binary_global) 
cv2.imshow("image", img) 
cv2.waitKey(0) 

Sie dann die img zum Schreiben usw. verwenden können

+0

Siehe auch http://scikit-image.org/docs/dev/user_guide/data_types.html # working-with-opencv –

+0

Das hat perfekt funktioniert. – Jess