Skip to content

Commit 9316618

Browse files
msk4862cclauss
authored andcommitted
digital_image_processing/convert_to_negative (TheAlgorithms#1216)
* digital_image_processing/convert_to_negative * added doc * added test code * Update convert_to_negative.py
1 parent 02b717e commit 9316618

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""
2+
Implemented an algorithm using opencv to convert a colored image into its negative
3+
"""
4+
5+
from cv2 import imread, imshow, waitKey, destroyAllWindows
6+
7+
8+
def convert_to_negative(img):
9+
# getting number of pixels in the image
10+
pixel_h, pixel_v = img.shape[0], img.shape[1]
11+
12+
# converting each pixel's color to its negative
13+
for i in range(pixel_h):
14+
for j in range(pixel_v):
15+
img[i][j] = [255, 255, 255] - img[i][j]
16+
17+
return img
18+
19+
20+
if __name__ == "__main__":
21+
# read original image
22+
img = imread("image_data/lena.jpg", 1)
23+
24+
# convert to its negative
25+
neg = convert_to_negative(img)
26+
27+
# show result image
28+
imshow("negative of original image", img)
29+
waitKey(0)
30+
destroyAllWindows()

digital_image_processing/test_digital_image_processing.py

+8
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,21 @@
88
import digital_image_processing.filters.sobel_filter as sob
99
import digital_image_processing.filters.convolve as conv
1010
import digital_image_processing.change_contrast as cc
11+
import digital_image_processing.convert_to_negative as cn
1112
from cv2 import imread, cvtColor, COLOR_BGR2GRAY
1213
from numpy import array, uint8
1314
from PIL import Image
1415

1516
img = imread(r"digital_image_processing/image_data/lena_small.jpg")
1617
gray = cvtColor(img, COLOR_BGR2GRAY)
1718

19+
# Test: convert_to_negative()
20+
def test_convert_to_negative():
21+
negative_img = cn.convert_to_negative(img)
22+
# assert negative_img array for at least one True
23+
assert negative_img.any()
24+
25+
1826
# Test: change_contrast()
1927
def test_change_contrast():
2028
with Image.open("digital_image_processing/image_data/lena_small.jpg") as img:

0 commit comments

Comments
 (0)