Skip to content

Commit d8cc369

Browse files
committed
Detect color in superpixel regions
colored_regions.py
1 parent 34f6111 commit d8cc369

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
20. [Detecting the centre of a geometric shape](https://github.com/yashk2000/Image-Processing#20-detecting-the-centre-of-a-geometric-shape)
2323
21. [Long Exposure](https://github.com/yashk2000/Image-Processing#21-long-exposure)
2424
22. [Segmentation into Superpixels](https://github.com/yashk2000/Image-Processing#22-segmentation-into-superpixels)
25+
23. [Colors in superpixels](https://github.com/yashk2000/Image-Processing#23-colors-in-superpixels)
2526

2627
# 1) Opening an image using openCV
2728

@@ -668,3 +669,11 @@ Image segmented into 200 superpixels
668669
Image segmented into 300 superpixels
669670

670671
![Screenshot from 2020-04-24 23-49-39](https://user-images.githubusercontent.com/41234408/80244625-e2617980-8686-11ea-9830-8693839bfb5f.png)
672+
673+
# 23) Colors in superpixels
674+
675+
The code for this can be found [here](https://github.com/yashk2000/Image-Processing/blob/master/colored_regions.py).
676+
677+
This helps in detecting the saturation of colors in different parts of an image that have been segmented into superpixels.
678+
679+
![Screenshot from 2020-04-26 13-27-49](https://user-images.githubusercontent.com/41234408/80301559-7ab04900-87c2-11ea-9a65-3528779f2be8.png)

colored_regions.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
from skimage.exposure import rescale_intensity
2+
from skimage.segmentation import slic
3+
from skimage.util import img_as_float
4+
from skimage import io
5+
import numpy as np
6+
import argparse
7+
import cv2
8+
9+
def segment_colorfulness(image, mask):
10+
(B, G, R) = cv2.split(image.astype("float"))
11+
R = np.ma.masked_array(R, mask=mask)
12+
G = np.ma.masked_array(B, mask=mask)
13+
B = np.ma.masked_array(B, mask=mask)
14+
15+
rg = np.absolute(R - G)
16+
17+
yb = np.absolute(0.5 * (R + G) - B)
18+
19+
stdRoot = np.sqrt((rg.std() ** 2) + (yb.std() ** 2))
20+
meanRoot = np.sqrt((rg.mean() ** 2) + (yb.mean() ** 2))
21+
22+
return stdRoot + (0.3 * meanRoot)
23+
24+
ap = argparse.ArgumentParser()
25+
ap.add_argument("-i", "--image", required=True)
26+
ap.add_argument("-s", "--segments", type=int, default=100)
27+
args = vars(ap.parse_args())
28+
29+
orig = cv2.imread(args["image"])
30+
vis = np.zeros(orig.shape[:2], dtype="float")
31+
32+
image = io.imread(args["image"])
33+
segments = slic(img_as_float(image), n_segments=args["segments"],
34+
slic_zero=True)
35+
36+
for v in np.unique(segments):
37+
mask = np.ones(image.shape[:2])
38+
mask[segments == v] = 0
39+
40+
C = segment_colorfulness(orig, mask)
41+
vis[segments == v] = C
42+
43+
vis = rescale_intensity(vis, out_range=(0, 255)).astype("uint8")
44+
45+
alpha = 0.6
46+
overlay = np.dstack([vis] * 3)
47+
output = orig.copy()
48+
cv2.addWeighted(overlay, alpha, output, 1 - alpha, 0, output)
49+
50+
cv2.imshow("Input", orig)
51+
cv2.imshow("Visualization", vis)
52+
cv2.imshow("Output", output)
53+
cv2.waitKey(0)

0 commit comments

Comments
 (0)