Skip to content

Commit 0429a5f

Browse files
committed
Update solutions for L6
1 parent d12e761 commit 0429a5f

11 files changed

+24
-26
lines changed

solutions/lab-6/.DS_Store

6 KB
Binary file not shown.

solutions/lab-6/SOLcs41img_bright.png

3.22 MB
Loading
2.99 MB
Loading
364 KB
Loading

solutions/lab-6/SOLcs41img_green.png

1.81 MB
Loading

solutions/lab-6/SOLcs41img_third.png

1.81 MB
Loading
3.34 MB
Loading
3.35 MB
Loading
3.35 MB
Loading

solutions/lab-6/cs41img.png

3.08 MB
Loading

solutions/lab-6/lab6-part1-numpy.ipynb

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
},
2121
{
2222
"cell_type": "code",
23-
"execution_count": 9,
23+
"execution_count": 1,
2424
"metadata": {},
2525
"outputs": [
2626
{
@@ -166,7 +166,7 @@
166166
},
167167
{
168168
"cell_type": "code",
169-
"execution_count": 8,
169+
"execution_count": 2,
170170
"metadata": {},
171171
"outputs": [],
172172
"source": [
@@ -332,17 +332,16 @@
332332
},
333333
{
334334
"cell_type": "code",
335-
"execution_count": 55,
335+
"execution_count": 8,
336336
"metadata": {},
337337
"outputs": [],
338338
"source": [
339339
"\n",
340340
"def brightness(img, factor):\n",
341-
"\tfor row in range(img.shape[0]):\n",
342-
"\t\tfor col in range(img.shape[1]):\n",
343-
"\t\t\tfor color in range(img.shape[2]):\n",
344-
"\t\t\t\timg[row, col, color] = min(img[row, col, color]*factor, 255)\n",
345-
"\treturn img\n",
341+
" with np.nditer(img, op_flags=['readwrite']) as it:\n",
342+
" for elem in it:\n",
343+
" elem[...] = min(elem*factor, 255)\n",
344+
" return img\n",
346345
"\n",
347346
"img = load_image(\"cs41img.png\").copy()\n",
348347
"save_image(brightness(img, 1.5), \"cs41img_bright.png\")"
@@ -364,20 +363,20 @@
364363
},
365364
{
366365
"cell_type": "code",
367-
"execution_count": 59,
366+
"execution_count": 11,
368367
"metadata": {},
369368
"outputs": [],
370369
"source": [
371370
"def contrast(img, factor):\n",
372-
"\tfor row in range(img.shape[0]):\n",
373-
"\t\tfor col in range(img.shape[1]):\n",
374-
"\t\t\tfor color in range(img.shape[2]):\n",
375-
"\t\t\t\tif img[row, col, color] < 128:\n",
376-
"\t\t\t\t\timg[row, col, color] = img[row, col, color]/factor\n",
377-
"\t\t\t\telse:\n",
378-
"\t\t\t\t\timg[row, col, color] = min(img[row, col, color]*factor, 255)\n",
371+
" with np.nditer(img, op_flags=['readwrite']) as it:\n",
372+
" for elem in it:\n",
373+
" elem[...] = min(elem*factor, 255)\n",
374+
" if elem < 128:\n",
375+
" elem[...] = elem/factor\n",
376+
" else:\n",
377+
" elem[...] = min(elem*factor, 255)\n",
379378
"\n",
380-
"\treturn img\n",
379+
" return img\n",
381380
"\n",
382381
"img = load_image(\"cs41img.png\").copy()\n",
383382
"save_image(contrast(img, 1.5), \"cs41img_contrast.png\")"
@@ -413,19 +412,18 @@
413412
},
414413
{
415414
"cell_type": "code",
416-
"execution_count": 57,
415+
"execution_count": 16,
417416
"metadata": {},
418417
"outputs": [],
419418
"source": [
420419
"def reconstruct(img1, img2, img3):\n",
421-
" reconstructed_img = np.zeros(img1.shape)\n",
422-
" for row in range(img1.shape[0]):\n",
423-
" for col in range(img1.shape[1]):\n",
424-
" for color in range(img1.shape[2]):\n",
425-
" pixel_choices = np.asarray([img1[row, col, color], img2[row, col, color], img3[row, col, color]])\n",
426-
" reconstructed_img[row, col, color] = np.argmax(np.bincount(pixel_choices))\n",
427-
"\n",
428-
" return reconstructed_img\n",
420+
" with np.nditer(img1, op_flags=['readwrite'], flags=['multi_index']) as it:\n",
421+
" for elem in it:\n",
422+
" pixel_choices = np.asarray([img1[it.multi_index], img2[it.multi_index], img3[it.multi_index]])\n",
423+
"\n",
424+
" elem[...] = np.argmax(np.bincount(pixel_choices))\n",
425+
"\n",
426+
" return img1\n",
429427
"\n",
430428
"img1 = load_image(\"SOLcs41img_unicorn1.png\").copy()\n",
431429
"img2 = load_image(\"SOLcs41img_unicorn2.png\").copy()\n",

0 commit comments

Comments
 (0)