Initial commit
This commit is contained in:
commit
ecac7eaa7e
3
.idea/.gitignore
vendored
Normal file
3
.idea/.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
7
.idea/discord.xml
Normal file
7
.idea/discord.xml
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="DiscordProjectSettings">
|
||||||
|
<option name="show" value="PROJECT_FILES" />
|
||||||
|
<option name="description" value="" />
|
||||||
|
</component>
|
||||||
|
</project>
|
12
.idea/inspectionProfiles/Project_Default.xml
Normal file
12
.idea/inspectionProfiles/Project_Default.xml
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
<component name="InspectionProjectProfileManager">
|
||||||
|
<profile version="1.0">
|
||||||
|
<option name="myName" value="Project Default" />
|
||||||
|
<inspection_tool class="PyPep8Inspection" enabled="true" level="WEAK WARNING" enabled_by_default="true">
|
||||||
|
<option name="ignoredErrors">
|
||||||
|
<list>
|
||||||
|
<option value="E501" />
|
||||||
|
</list>
|
||||||
|
</option>
|
||||||
|
</inspection_tool>
|
||||||
|
</profile>
|
||||||
|
</component>
|
6
.idea/inspectionProfiles/profiles_settings.xml
Normal file
6
.idea/inspectionProfiles/profiles_settings.xml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<component name="InspectionProjectProfileManager">
|
||||||
|
<settings>
|
||||||
|
<option name="USE_PROJECT_PROFILE" value="false" />
|
||||||
|
<version value="1.0" />
|
||||||
|
</settings>
|
||||||
|
</component>
|
4
.idea/misc.xml
Normal file
4
.idea/misc.xml
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9" project-jdk-type="Python SDK" />
|
||||||
|
</project>
|
8
.idea/modules.xml
Normal file
8
.idea/modules.xml
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/opencv_testing.iml" filepath="$PROJECT_DIR$/.idea/opencv_testing.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
8
.idea/opencv_testing.iml
Normal file
8
.idea/opencv_testing.iml
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="PYTHON_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager">
|
||||||
|
<content url="file://$MODULE_DIR$" />
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
BIN
bendzo.jpg
Normal file
BIN
bendzo.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 25 KiB |
BIN
image.jpg
Normal file
BIN
image.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 4.8 KiB |
107
minimal_energy.py
Normal file
107
minimal_energy.py
Normal file
@ -0,0 +1,107 @@
|
|||||||
|
import pygame
|
||||||
|
from numpy import array as a
|
||||||
|
import numpy as np
|
||||||
|
from PIL import Image
|
||||||
|
|
||||||
|
im = Image.open("image.jpg")
|
||||||
|
gray = im.convert('LA')
|
||||||
|
asnumpy_gray = np.asarray(gray)
|
||||||
|
image = np.asarray(im)
|
||||||
|
|
||||||
|
sobel_0 = [
|
||||||
|
[-0.125, 0, 0.125],
|
||||||
|
[-0.25, 0, 0.25],
|
||||||
|
[-0.125, 0, 0.125]
|
||||||
|
]
|
||||||
|
|
||||||
|
sobel_1 = [
|
||||||
|
[-0.125, -0.25, -0.125],
|
||||||
|
[0, 0, 0],
|
||||||
|
[0.125, 0.25, 0.125]
|
||||||
|
]
|
||||||
|
|
||||||
|
edge_values = a([
|
||||||
|
a([0, 1, 0.5, 0.8]),
|
||||||
|
a([0.5, 0.2, 0.3, 0.4]),
|
||||||
|
a([1, 0.5, 0.6, 0.7]),
|
||||||
|
a([0.2, 0.1, 0.1, 0.1])
|
||||||
|
])
|
||||||
|
|
||||||
|
asnumpy_gray = a([a([pixel[0] for pixel in row]) for row in asnumpy_gray])
|
||||||
|
image = [row.tolist() for row in image]
|
||||||
|
|
||||||
|
|
||||||
|
def get_minimal_energy_map_vertical(edge_values_):
|
||||||
|
|
||||||
|
minimal_energy_map = np.zeros(shape=edge_values_.shape)
|
||||||
|
minimal_energy_map[-1] = edge_values_[-1]
|
||||||
|
|
||||||
|
for i in range(-2, -len(edge_values_) - 1, -1):
|
||||||
|
for j in range(len(edge_values_[0])):
|
||||||
|
if j == 0:
|
||||||
|
minimal_energy_map[i][j] = edge_values_[i][j] + min(minimal_energy_map[i + 1][j:j + 2])
|
||||||
|
elif j == len(edge_values_[0]) - 1:
|
||||||
|
minimal_energy_map[i][j] = edge_values_[i][j] + min(minimal_energy_map[i + 1][j - 1:j + 1])
|
||||||
|
else:
|
||||||
|
minimal_energy_map[i][j] = edge_values_[i][j] + min(minimal_energy_map[i + 1][j - 1:j + 2])
|
||||||
|
|
||||||
|
return minimal_energy_map
|
||||||
|
|
||||||
|
|
||||||
|
def seam_carve_vertical(image, minimal_energy_map):
|
||||||
|
seam = np.zeros(len(minimal_energy_map), dtype=np.int32)
|
||||||
|
|
||||||
|
seam[0] = (minimal_energy_map[0].tolist()).index(min(minimal_energy_map[0]))
|
||||||
|
|
||||||
|
for i in range(len(minimal_energy_map) - 1):
|
||||||
|
j = seam[i]
|
||||||
|
|
||||||
|
if j == 0:
|
||||||
|
sub_array = minimal_energy_map[i + 1][:2].tolist()
|
||||||
|
seam[i + 1] = sub_array.index(min(sub_array))
|
||||||
|
elif j == len(minimal_energy_map[0]) - 1:
|
||||||
|
sub_array = minimal_energy_map[i + 1][-2:].tolist()
|
||||||
|
seam[i + 1] = j + sub_array.index(min(sub_array)) - 1
|
||||||
|
else:
|
||||||
|
sub_array = minimal_energy_map[i + 1][j - 1:j + 2].tolist()
|
||||||
|
seam[i + 1] = j + sub_array.index(min(sub_array)) - 1
|
||||||
|
|
||||||
|
for i in range(len(seam)):
|
||||||
|
image[i] = image[i].tolist()[:seam[i]] + image[i].tolist()[seam[i] + 1:]
|
||||||
|
|
||||||
|
return image
|
||||||
|
|
||||||
|
|
||||||
|
def get_edges_values(input):
|
||||||
|
img1 = np.zeros(shape=asnumpy_gray.shape)
|
||||||
|
for y in range(1, len(img1) - 1):
|
||||||
|
for x in range(1, len(img1[y]) - 1):
|
||||||
|
inx = 0.
|
||||||
|
for ax in range(-1, 2):
|
||||||
|
for b in range(-1, 2):
|
||||||
|
try:
|
||||||
|
inx += asnumpy_gray[y + ax][x + b] * sobel_0[ax + 1][b + 1] + asnumpy_gray[y + ax][x + b] * sobel_1[ax + 1][b + 1]
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
img1[y][x] = inx
|
||||||
|
return img1
|
||||||
|
|
||||||
|
|
||||||
|
def dostuff(input):
|
||||||
|
for y in range(len(input)):
|
||||||
|
for x in range(len(input[y])):
|
||||||
|
input[y][x] = (((input[y][x] - -9635.0) * (255 - 0)) / (255 - -9635.0)) + 0
|
||||||
|
|
||||||
|
return input
|
||||||
|
|
||||||
|
|
||||||
|
tmp = get_minimal_energy_map_vertical(get_edges_values(asnumpy_gray))
|
||||||
|
|
||||||
|
tmp = dostuff(tmp)
|
||||||
|
|
||||||
|
image = seam_carve_vertical(image, tmp)
|
||||||
|
|
||||||
|
out = Image.fromarray(image)
|
||||||
|
|
||||||
|
out.show()
|
Loading…
Reference in New Issue
Block a user