Skip to content

Commit 324c1fe

Browse files
modifification des types de matrices en uint8
1 parent 83d4766 commit 324c1fe

9 files changed

Lines changed: 116 additions & 13 deletions

File tree

imgprocess/img_api.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def _convolution(request):
136136
conv_matrix[i,]=elts
137137

138138
image_info=save_image(request.FILES["imguploaded"],img_path)
139-
new_img_matrix=ImgLib().convolution(image_info["matrix"],conv_matrix)
139+
new_img_matrix=ImgLib().convolution2(image_info["matrix"],conv_matrix)
140140

141141
new_img_matrix=new_img_matrix.astype(np.uint8)
142142
new_img=Image.fromarray(new_img_matrix)
@@ -180,3 +180,28 @@ def _interpolation(request):
180180
image_info["list"]=None
181181
image_info["pgm_name"]=new_image_name+".pgm"
182182
return JsonResponse(image_info)
183+
184+
def _median_filter(request):
185+
voisinage=int(request.POST.get("voisinage",0))
186+
img_info=save_image(request.FILES["imguploaded"],None)
187+
median_matrix=ImgLib.median_filter(img_info["matrix"],voisinage)
188+
189+
new_median_matrix=median_matrix.astype(np.uint8)
190+
new_img=Image.fromarray(new_median_matrix)
191+
#on definit un nouveau nom pour notre image
192+
extension=img_info["extension"]
193+
if(extension.lower()==".pgm"):
194+
extension=".png"
195+
196+
new_image_name=get_random_string(24)+extension
197+
198+
new_img.save(result_path+new_image_name)
199+
200+
#sauvegarde au format pgm de l'image
201+
ImgLib.save_matrix_as_pgm(new_median_matrix,new_image_name)
202+
203+
img_info["matrix"]=None
204+
img_info["saved_name"]=new_image_name
205+
img_info["list"]=None
206+
img_info["pgm_name"]=new_image_name+".pgm"
207+
return JsonResponse(img_info)

imgprocess/static/shared/js/img_process.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ function showPreview(objFileInput, html_element) {
1010

1111

1212
$(function (e) {
13+
//on va forcer le telechargement des fichiers pgm
14+
$("#download-pgm-result").click(function(){
15+
var dname=$(this).attr("data-download")
16+
$.get($(this).attr("data-url"),{download_name:dname},function(e){
17+
console.log("bojnour")
18+
});
19+
});
20+
1321
$("#image-viewver").hide();
1422
$("#image-viewver").click(function () {
1523
$(this).fadeOut(1000)
@@ -32,6 +40,7 @@ $(function (e) {
3240
success: function (result) {
3341
console.log(result);
3442
$("#result-box").html("<img src='/static/imageprocess/images/result/" + result["saved_name"] + "' width='100%' height='100%' />");
43+
$("#download-pgm-result").attr("data-download",result["pgm_name"])
3544
},
3645
error: function (result) {
3746
console.log("error");

imgprocess/templates/imgprocess/constraste.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ <h4>Result</h4>
3737
<div class="card border-0 shadow-lg" id="result-box">
3838

3939
</div>
40+
<button type="button" data-url="{% url 'imageprocess:download' %}" data-download="" id="download-pgm-result" class="btn btn-warning btn-lg text-white">Download PGM RESULT</button>
4041
</div>
4142
</div>
4243
<br />

imgprocess/templates/imgprocess/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ <h3>EGALISATION HISTOGRAMME</h3>
2828
</div>
2929

3030
<div class="col-lg-3 col-12">
31-
<a class="nav-link" href="{% url 'imageprocess:egalisation' %}">
31+
<a class="nav-link" href="{% url 'imageprocess:median' %}">
3232
<div class="card shadow-lg border-0">
3333

3434
<h3>FILTRE MEDIAN</h3>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{% extends 'base.html' %}
2+
3+
4+
{% block content %}
5+
<br />
6+
<div class="container">
7+
<h1>Welcome to median filter</h1>
8+
<hr />
9+
<br />
10+
<form class="" id="form_upload_image" enctype="multipart/form-data" method="post" action="{% url 'imageprocess:pmedian' %}">
11+
{% csrf_token %}
12+
<div class="row">
13+
<div class="col-12">
14+
<!-- <textarea class="form-control form-control-lg"> -->
15+
</textarea>
16+
</div>
17+
<div class="col">
18+
<label>Neighborhood</label>
19+
<input type="number" name="voisinage" placeholder="
20+
neighborhood of 3 means 3x3 so 9" value="0" max="254" min="0" required="true" class="form-control" />
21+
</div>
22+
</div>
23+
<div class="row img-list-work">
24+
<div class="col-6">
25+
<h4>Preview</h4>
26+
<div class="card border-0 shadow-lg" id="img_preview">
27+
28+
</div>
29+
<label>upload image</label>
30+
<input type="file" name="imguploaded" class="form-control form-control-file" onChange="showPreview(this,'#img_preview')" />
31+
</div>
32+
<div class="col-6">
33+
<h4>Result</h4>
34+
<div class="card border-0 shadow-lg" id="result-box">
35+
36+
</div>
37+
<button type="button" data-url="{% url 'imageprocess:download' %}" data-download="" id="download-pgm-result" class="btn btn-warning btn-lg text-white">Download PGM RESULT</button>
38+
</div>
39+
</div>
40+
<br />
41+
<br />
42+
<button type="submit" class="btn btn-info rounded btn-lg second-color shadow-lg text-white">MEDIAN FILTER</button>
43+
</form>
44+
</div>
45+
{% endblock content %}

imgprocess/urls.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,8 @@
1717
path("pconvolution",img_api._convolution,name="pconvolution"),
1818

1919
path("interpolation",views.interpolation,name="interpolation"),
20-
path("pinterpolation",img_api._interpolation,name="pinterpolation")
20+
path("pinterpolation",img_api._interpolation,name="pinterpolation"),
21+
path("download",views.download_file,name="download"),
22+
path("median_filter",views.median_filter,name="median"),
23+
path("pmedian_filter",img_api._median_filter,name="pmedian")
2124
]

imgprocess/utils/img_lib.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def linea_transformation(cls,img_matrix,saturation_min=0,saturation_max=0):
9191
differecence=max_pixel-min_pixel
9292
lumine=np.mean(img_matrix)
9393
dimesnion=img_matrix.shape
94-
new_image=np.zeros(dimesnion,dtype=int)
94+
new_image=np.zeros(dimesnion,dtype="uint8")
9595
for i in range(dimesnion[0]):
9696
for j in range(dimesnion[1]):
9797
new_image[i][j]=int((255/differecence)*(img_matrix[i][j]-min_pixel))
@@ -154,7 +154,7 @@ def add_two_image(cls,img_matrix1,img_matrix2,mult_fact_1=1,mult_fact_2=1):
154154
"""That function take two image matrix in parameter, supooe to be the same and
155155
make the summ"""
156156
dimension=img_matrix1.shape
157-
new_matrix=np.zeros(dimension)
157+
new_matrix=np.zeros(dimension,dtype="uint8")
158158
for i in range(dimension[0]):
159159
for j in range(dimension[1]):
160160
pixel=img_matrix1[i][j]*mult_fact_1+img_matrix2[i][j]*mult_fact_2
@@ -168,7 +168,7 @@ def subtract_two_image(cls,img_matrix1,img_matrix2,mult_fact_1=1,mult_fact_2=1):
168168
"""That function take two image matrix in parameter, supooe to be the same and
169169
make the subtraction"""
170170
dimension=img_matrix1.shape
171-
new_matrix=np.zeros(dimension)
171+
new_matrix=np.zeros(dimension,dtype="uint8")
172172
for i in range(dimension[0]):
173173
for j in range(dimension[1]):
174174
pixel=img_matrix1[i][j]*mult_fact_1-img_matrix2[i][j]*mult_fact_2
@@ -182,7 +182,7 @@ def subtract_two_image(cls,img_matrix1,img_matrix2,mult_fact_1=1,mult_fact_2=1):
182182
def or_operation(cls,img_matrix1,img_matrix2):
183183
"""Take to image matrix and make the or operation"""
184184
dimension=img_matrix1.shape
185-
new_matrix=np.zeros(dimension)
185+
new_matrix=np.zeros(dimension,dtype="uint8")
186186
for i in range(dimension[0]):
187187
for j in range(dimension[1]):
188188
new_matrix[i][j]=int(img_matrix1[i][j])|int(img_matrix2[i][j])
@@ -192,7 +192,7 @@ def or_operation(cls,img_matrix1,img_matrix2):
192192
def and_operation(cls,img_matrix1,img_matrix2):
193193
"""Take to image matrix and make the and operation"""
194194
dimension=img_matrix1.shape
195-
new_matrix=np.zeros(dimension)
195+
new_matrix=np.zeros(dimension,dtype="uint8")
196196
for i in range(dimension[0]):
197197
for j in range(dimension[1]):
198198
new_matrix[i][j]=int(img_matrix1[i][j]) & int(img_matrix2[i][j])
@@ -230,13 +230,13 @@ def convolution2(cls,img_matrix,kernel):
230230
center=int((kernel.shape[1]-1)/2)
231231
dimension=img_matrix.shape
232232
kernel_shape=kernel.shape
233-
new_img_matrix=np.zeros(img_matrix.shape,dtype=int)
233+
new_img_matrix=np.zeros(img_matrix.shape,dtype="uint8")
234234
for i in range(center,dimension[0]-center):
235235
for j in range(center,dimension[1]-center):
236236
pixel_somme=0
237237
for u in range(kernel_shape[0]):
238238
for v in range(kernel_shape[1]):
239-
pixel_somme+=img_matrix[i-u][j-v]*kernel[u][v]
239+
pixel_somme+=img_matrix[i-u-1][j-v-1]*kernel[u][v]
240240
pixel_somme=int(pixel_somme)
241241
if(pixel_somme<0):
242242
pixel_somme=0
@@ -283,7 +283,17 @@ def save_matrix_as_pgm(cls,img_matrix,name):
283283
with open("imgprocess/static/imageprocess/images/result/"+name+".pgm","w+") as f:
284284
f.write(data_to_write)
285285

286-
286+
@classmethod
287+
def median_filter(cls,img_matrix,voisinage):
288+
center=int(voisinage/2)
289+
dimension=img_matrix.shape
290+
new_img_matrix=np.zeros(img_matrix.shape,dtype=int)
291+
for i in range(center,dimension[0]-center):
292+
for j in range(center,dimension[1]-center):
293+
voisinnage=img_matrix[i-center:i+center+1,j-center:j+center+1]
294+
#print(voisinnage.shape)
295+
new_img_matrix[i][j]=np.median(np.reshape(voisinnage,voisinage*voisinage))
296+
return new_img_matrix
287297

288298

289299

imgprocess/utils/utility.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
import numpy as np
77
import random,math
88

9+
img_path="imgprocess/static/imageprocess/images/"
10+
hist_path=img_path+"hist/"
11+
hist_path_result=img_path+"histresult/"
12+
result_path=img_path+"result/"
913

1014
def get_random_string(length):
1115
letters = string.ascii_lowercase

imgprocess/views.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@
1010
import numpy as np
1111
# Create your views here.
1212

13-
def download_file(request,path):
14-
pass
13+
def download_file(request):
14+
file_name=request.GET["download_name"]
15+
response =FileResponse(open(result_path+file_name,"r"))
16+
17+
return response
1518

1619
def index(request):
1720
return render(request,"imgprocess/index.html")
@@ -48,3 +51,6 @@ def author(request):
4851

4952
def interpolation(request):
5053
return render(request,"imgprocess/interpolation.html")
54+
55+
def median_filter(request):
56+
return render(request,"imgprocess/median_filter.html")

0 commit comments

Comments
 (0)