Tips for Python beginners to use the Scikit-image example for themselves
Tips for Python beginners to use the Scikit-image example for themselves 2 Process multiple files
Next, I will write the content for Python beginners to play with the example of Scikit-image with a slight modification.
Continue to Normalized Cut
Will be on the agenda.
In [Tips 2 for Python beginners to use the Scikit-image example for themselves, processing multiple files], I was able to process various images.
However, you may be dissatisfied because the result cannot be saved in the file as it is.
So I would like to give you some tips for saving to a file.
cv2.imwrite("out.png ", img) OpenCV-Python Tutorials >> Getting Started with Images
python
for i in range(len(names)):
img = cv2.imread(names[i])
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
oname = "gray_%04d.png " % i
cv2.imwrite(oname, gray)
The function equivalent to the formatting of prinft () / sprinft () / fprintf () in C language is Python standard library / string format operation It is in.
os.path.dirname() os.path.basename() os.path.splitext() os.path.join()
Python standard library / os.path — common path name operation
If the output directory odir does not exist, It is customary to create a directory as follows:
python
if not os.path.isdir(odir):
os.makedirs(odir)
Python standard library / os — miscellaneous operating system interfaces
os.makedirs (path [, mode]) Recursive directory creation function. Similar to mkdir (), but creates all the intermediate directories needed to create the leaf directory.
oname = os.path.join(odir, "img_%05d.png " % i) If you include the output directory in the output file name like You can output to a directory other than the script execution directory.
The output file name can also be generated from the input file name by the replace function. The string has a replace () method, which returns the replaced string as a return value. replace(old, new[, count]) Python standard library / string methods
Using these functions, you can freely set the file name of the output destination.
"Hint: Let's process multiple files using for name in glob.glob (" * .jpg "):" Let's try to save the result to a file based on the example shown in. Condition: Create an output directory "out" and save the file there. Condition: For the input "image.jpg ", save it with the file name "out / image_ncut.png ".
You should be able to code with the hints given so far. Enjoy the OpenCV-Python, scikit-image, scikit-learn libraries even if you are new to Python.
-When writing directory delimiters in Python, you can use the "/" character like "abc / def.txt" even on Windows. -If you want to use the delimiter with "", it is convenient to add the symbol r before "" like name = r "abc \ def.txt". In that case, you don't have to write name = "abc \\ def.txt" (two "" characters are written).
https://docs.python.jp/3/library/pathlib.html Seems to be better to use.
python
# -*- coding: utf-8 -*-
# pylint: disable=C0103
import pathlib
from skimage import io, segmentation, color
from skimage.future import graph
def plotNcut(img):
"""
img: color img
"""
labels1 = segmentation.slic(img, compactness=30, n_segments=400)
g = graph.rag_mean_color(img, labels1, mode='similarity')
labels2 = graph.cut_normalized(labels1, g)
return color.label2rgb(labels2, img, kind='avg'), labels2
if __name__ == "__main__":
oDir = pathlib.Path("out")
if not oDir.is_dir():
oDir.mkdir()
for p in pathlib.Path("../images").glob("*.png "):
print(p)
oname = oDir.joinpath(p.with_suffix(".jpg ").name)
img = io.imread(p)
out, _ = plotNcut(img)
io.imsave(str(oname), out)
print(oname)
Recommended Posts