|
749 | 749 | "%%ipytest\n", |
750 | 750 | "\n", |
751 | 751 | "import cv2\n", |
752 | | - "def solution_scale_image(img, scale_factor: float):\n", |
| 752 | + "import numpy as np\n", |
| 753 | + "def solution_scale_image(img: np.ndarray, scale_factor: float):\n", |
| 754 | + " \"\"\"\n", |
| 755 | + " The function takes an image as input and rescales it to a new dimension.\n", |
| 756 | + " For that, you need to compute the new dimensions of the image using the scale factor \n", |
| 757 | + " and then use OpenCV's `cv2.resize` function to resize the image, e.g., `cv2.resize(img, (new_width, new_height))`.\n", |
| 758 | + "\n", |
| 759 | + " Args:\n", |
| 760 | + " img (np.ndarray): The input image.\n", |
| 761 | + " scale_factor (float): The factor by which to scale the image.\n", |
| 762 | + "\n", |
| 763 | + " Returns:\n", |
| 764 | + " np.ndarray: The scaled image.\n", |
| 765 | + " \"\"\"\n", |
753 | 766 | " # Start your code here\n", |
754 | 767 | " return\n", |
755 | 768 | " # End your code here" |
|
798 | 811 | "%%ipytest\n", |
799 | 812 | "\n", |
800 | 813 | "import cv2\n", |
801 | | - "def solution_crop_image(img, x: int, y: int, width: int, height: int):\n", |
| 814 | + "import numpy as np\n", |
| 815 | + "def solution_crop_image(img: np.ndarray, x: int, y: int, width: int, height: int):\n", |
| 816 | + " \"\"\"\n", |
| 817 | + " The function takes an image as input and crops it to a specified rectangular region.\n", |
| 818 | + " In OpenCV, images are represented as NumPy arrays. You can crop the image by \n", |
| 819 | + " using array slicing, keeping in mind that the first dimension is the y-axis (rows) \n", |
| 820 | + " and the second dimension is the x-axis (columns): `img[y_start:y_end, x_start:x_end]`. \n", |
| 821 | + " You will need to calculate these start and end coordinates using the provided \n", |
| 822 | + " x, y, width, and height parameters.\n", |
| 823 | + "\n", |
| 824 | + " Args:\n", |
| 825 | + " img (np.ndarray): The input image.\n", |
| 826 | + " x (int): The starting x-coordinate (column) of the crop.\n", |
| 827 | + " y (int): The starting y-coordinate (row) of the crop.\n", |
| 828 | + " width (int): The width of the cropped region.\n", |
| 829 | + " height (int): The height of the cropped region.\n", |
| 830 | + "\n", |
| 831 | + " Returns:\n", |
| 832 | + " np.ndarray: The cropped image.\n", |
| 833 | + " \"\"\"\n", |
802 | 834 | " # Start your code here\n", |
803 | 835 | " return\n", |
804 | 836 | " # End your code here" |
|
847 | 879 | "%%ipytest\n", |
848 | 880 | "\n", |
849 | 881 | "import cv2\n", |
850 | | - "def solution_horizontal_flip_image(img):\n", |
| 882 | + "import numpy as np\n", |
| 883 | + "def solution_horizontal_flip_image(img: np.ndarray):\n", |
| 884 | + " \"\"\"\n", |
| 885 | + " The function takes an image as input and flips it horizontally (creating a mirror image).\n", |
| 886 | + " To do this, use OpenCV's `cv2.flip(src, flipCode)` function. The `flipCode` integer \n", |
| 887 | + " determines the axis to flip around: \n", |
| 888 | + " - Use 1 for a horizontal flip (around the y-axis).\n", |
| 889 | + " - Use 0 for a vertical flip (around the x-axis).\n", |
| 890 | + " - Use -1 for both axes.\n", |
| 891 | + "\n", |
| 892 | + " Args:\n", |
| 893 | + " img (np.ndarray): The input image.\n", |
| 894 | + "\n", |
| 895 | + " Returns:\n", |
| 896 | + " np.ndarray: The horizontally flipped image.\n", |
| 897 | + " \"\"\"\n", |
851 | 898 | " # Start your code here\n", |
852 | 899 | " return\n", |
853 | 900 | " # End your code here" |
|
896 | 943 | "%%ipytest\n", |
897 | 944 | "\n", |
898 | 945 | "import cv2\n", |
899 | | - "def solution_vertical_flip_image(img):\n", |
| 946 | + "import numpy as np\n", |
| 947 | + "def solution_vertical_flip_image(img: np.ndarray):\n", |
| 948 | + " \"\"\"\n", |
| 949 | + " The function takes an image as input and flips it vertically.\n", |
| 950 | + " To do this, use OpenCV's `cv2.flip(src, flipCode)` function. The `flipCode` integer \n", |
| 951 | + " determines the axis to flip around: \n", |
| 952 | + " - Use 1 for a horizontal flip (around the y-axis).\n", |
| 953 | + " - Use 0 for a vertical flip (around the x-axis).\n", |
| 954 | + " - Use -1 for both axes.\n", |
| 955 | + "\n", |
| 956 | + " Args:\n", |
| 957 | + " img (np.ndarray): The input image.\n", |
| 958 | + "\n", |
| 959 | + " Returns:\n", |
| 960 | + " np.ndarray: The vertically flipped image.\n", |
| 961 | + " \"\"\"\n", |
900 | 962 | " # Start your code here\n", |
901 | 963 | " return\n", |
902 | 964 | " # End your code here" |
|
945 | 1007 | "%%ipytest\n", |
946 | 1008 | "\n", |
947 | 1009 | "import cv2\n", |
948 | | - "def solution_rotate_image(img, angle: float):\n", |
| 1010 | + "import numpy as np\n", |
| 1011 | + "def solution_rotate_image(img: np.ndarray, angle: float):\n", |
| 1012 | + " \"\"\"\n", |
| 1013 | + " The function takes an image as input and rotates it by a specified angle. \n", |
| 1014 | + " To ensure the corners of the image are not cropped after rotation, you must \n", |
| 1015 | + " calculate a new bounding box and adjust the rotation matrix. \n", |
| 1016 | + " \n", |
| 1017 | + " Follow these steps:\n", |
| 1018 | + " 1. Get the height and width.\n", |
| 1019 | + " 2. Find the center point of the image.\n", |
| 1020 | + " 3. Get the starting matrix: `mat = cv2.getRotationMatrix2D(center, angle, 1.0)`\n", |
| 1021 | + " \n", |
| 1022 | + " 4. Calculate the new canvas size using this trigonometry:\n", |
| 1023 | + " cos = np.abs(mat[0, 0])\n", |
| 1024 | + " sin = np.abs(mat[0, 1])\n", |
| 1025 | + " new_w = int((h * sin) + (w * cos))\n", |
| 1026 | + " new_h = int((h * cos) + (w * sin))\n", |
| 1027 | + " \n", |
| 1028 | + " 5. Adjust the matrix so the image is shifted to the middle of the new canvas:\n", |
| 1029 | + " mat[0, 2] += (new_w / 2) - center[0]\n", |
| 1030 | + " mat[1, 2] += (new_h / 2) - center[1]\n", |
| 1031 | + " \n", |
| 1032 | + " 6. Return the final rotated image using: `cv2.warpAffine(img, mat, (new_w, new_h))`\n", |
| 1033 | + "\n", |
| 1034 | + " Args:\n", |
| 1035 | + " img (np.ndarray): The input image.\n", |
| 1036 | + " angle (float): The angle of rotation in degrees.\n", |
| 1037 | + "\n", |
| 1038 | + " Returns:\n", |
| 1039 | + " np.ndarray: The rotated image on a properly sized canvas.\n", |
| 1040 | + " \"\"\"\n", |
949 | 1041 | " # Start your code here\n", |
950 | 1042 | " return\n", |
951 | 1043 | " # End your code here" |
|
1009 | 1101 | "%%ipytest\n", |
1010 | 1102 | "\n", |
1011 | 1103 | "import cv2\n", |
1012 | | - "def solution_average_filter(img, kernel_size = (5, 5)):\n", |
| 1104 | + "import numpy as np\n", |
| 1105 | + "def solution_average_filter(img: np.ndarray, kernel_size: tuple = (5, 5)):\n", |
| 1106 | + " \"\"\"\n", |
| 1107 | + " Applies an average filter to blur the image using a specific kernel size.\n", |
| 1108 | + " For that, you need to use OpenCV's `cv2.blur()` function, which takes the image and the kernel size as arguments.\n", |
| 1109 | + " `cv2.blur()` requires two arguments: the image, and the size of the kernel.\n", |
| 1110 | + " \n", |
| 1111 | + " Args:\n", |
| 1112 | + " img (np.ndarray): The input image.\n", |
| 1113 | + " kernel_size (tuple): The width and height of the blurring window. Default is (5, 5).\n", |
| 1114 | + "\n", |
| 1115 | + " Returns:\n", |
| 1116 | + " np.ndarray: The blurred image.\n", |
| 1117 | + " \"\"\"\n", |
1013 | 1118 | " # Start your code here\n", |
1014 | 1119 | " return\n", |
1015 | 1120 | " # End your code here" |
|
1058 | 1163 | "%%ipytest\n", |
1059 | 1164 | "\n", |
1060 | 1165 | "import cv2\n", |
1061 | | - "def solution_median_filter(img, ksize):\n", |
| 1166 | + "import numpy as np\n", |
| 1167 | + "def solution_median_filter(img: np.ndarray, ksize: int):\n", |
| 1168 | + " \"\"\"\n", |
| 1169 | + " Applies a median filter to the image using a specific kernel size.\n", |
| 1170 | + " For that, you need to use OpenCV's `cv2.medianBlur()` function, which takes the image and the kernel size as arguments.\n", |
| 1171 | + " `cv2.medianBlur()` requires two arguments: the image, and the size of the kernel.\n", |
| 1172 | + " \n", |
| 1173 | + " Args:\n", |
| 1174 | + " img (np.ndarray): The input image.\n", |
| 1175 | + " ksize (int): The size of the median filter kernel. Must be a positive odd integer.\n", |
| 1176 | + "\n", |
| 1177 | + " Returns:\n", |
| 1178 | + " np.ndarray: The blurred image.\n", |
| 1179 | + " \"\"\"\n", |
1062 | 1180 | " # Start your code here\n", |
1063 | 1181 | " return\n", |
1064 | 1182 | " # End your code here" |
|
1107 | 1225 | "%%ipytest\n", |
1108 | 1226 | "\n", |
1109 | 1227 | "import cv2\n", |
1110 | | - "def solution_gaussian_filter(img, kernel_size = (5, 5), sigma = 0):\n", |
| 1228 | + "import numpy as np\n", |
| 1229 | + "def solution_gaussian_filter(img: np.ndarray, kernel_size: tuple = (5, 5), sigma: float = 0):\n", |
| 1230 | + " \"\"\"\n", |
| 1231 | + " Applies a Gaussian filter to the image using a specific kernel size and sigma value.\n", |
| 1232 | + " For that, you need to use OpenCV's `cv2.GaussianBlur()` function, which takes the image, kernel size, and sigma as arguments.\n", |
| 1233 | + " `cv2.GaussianBlur()` requires three arguments: the image, the size of the kernel, and the sigma value.\n", |
| 1234 | + "\n", |
| 1235 | + " Args:\n", |
| 1236 | + " img (np.ndarray): The input image.\n", |
| 1237 | + " kernel_size (tuple): The width and height of the Gaussian kernel. Default is (5, 5).\n", |
| 1238 | + " sigma (float): The standard deviation of the Gaussian kernel. Default is 0.\n", |
| 1239 | + "\n", |
| 1240 | + " Returns:\n", |
| 1241 | + " np.ndarray: The blurred image.\n", |
| 1242 | + " \"\"\"\n", |
1111 | 1243 | " # Start your code here\n", |
1112 | 1244 | " return\n", |
1113 | 1245 | " # End your code here" |
|
1170 | 1302 | "%%ipytest\n", |
1171 | 1303 | "\n", |
1172 | 1304 | "import cv2\n", |
1173 | | - "def solution_adjust_brightness(img, brightness_value):\n", |
| 1305 | + "import numpy as np\n", |
| 1306 | + "def solution_adjust_brightness(img: np.ndarray, brightness_value: float):\n", |
| 1307 | + " \"\"\"\n", |
| 1308 | + " Adjusts the brightness of the image by adding a specified value to all pixel intensities.\n", |
| 1309 | + " To adjust the brightness, you can use OpenCV's `cv2.convertScaleAbs()` function, which scales, calculates absolute values, and converts the result to 8-bit.\n", |
| 1310 | + " `cv2.convertScaleAbs()` requires three arguments: the image, the alpha value (which is 1 for no scaling), and the beta value (which is the brightness adjustment value).\n", |
| 1311 | + " Args:\n", |
| 1312 | + " img (np.ndarray): The input image.\n", |
| 1313 | + " brightness_value (float): The value to add to the pixel intensities. Positive values increase brightness, while negative values decrease it.\n", |
| 1314 | + " Returns:\n", |
| 1315 | + " np.ndarray: The brightness-adjusted image.\n", |
| 1316 | + " \"\"\"\n", |
1174 | 1317 | " # Start your code here\n", |
1175 | 1318 | " return\n", |
1176 | 1319 | " # End your code here" |
|
1222 | 1365 | "%%ipytest\n", |
1223 | 1366 | "\n", |
1224 | 1367 | "import cv2\n", |
1225 | | - "def solution_adjust_contrast(img, contrast_value):\n", |
| 1368 | + "import numpy as np\n", |
| 1369 | + "def solution_adjust_contrast(img: np.ndarray, contrast_value: float):\n", |
| 1370 | + " \"\"\"\n", |
| 1371 | + " Adjusts the contrast of the image by scaling the pixel intensities.\n", |
| 1372 | + " To adjust the contrast, you can use OpenCV's `cv2.convertScaleAbs()` function, which scales, calculates absolute values, and converts the result to 8-bit.\n", |
| 1373 | + " `cv2.convertScaleAbs()` requires three arguments: the image, the alpha value (which is the contrast adjustment value), and the beta value (which is 0 for no additional brightness adjustment).\n", |
| 1374 | + " Args:\n", |
| 1375 | + " img (np.ndarray): The input image.\n", |
| 1376 | + " contrast_value (float): The value to scale the pixel intensities. Values greater than 1 increase contrast, while values between 0 and 1 decrease it.\n", |
| 1377 | + " Returns:\n", |
| 1378 | + " np.ndarray: The contrast-adjusted image.\n", |
| 1379 | + " \"\"\"\n", |
1226 | 1380 | " # Start your code here\n", |
1227 | 1381 | " return\n", |
1228 | 1382 | " # End your code here" |
|
1274 | 1428 | "%%ipytest\n", |
1275 | 1429 | "\n", |
1276 | 1430 | "import cv2\n", |
1277 | | - "def solution_adjust_saturation(img, saturation_factor):\n", |
| 1431 | + "import numpy as np\n", |
| 1432 | + "def solution_adjust_saturation(img: np.ndarray, saturation_factor: float):\n", |
| 1433 | + " \"\"\"\n", |
| 1434 | + " Adjusts the saturation of the image by modifying the saturation channel in the HSV color space.\n", |
| 1435 | + " To do that you need to convert the image from RGB to HSV, adjust the saturation channel, and then convert it back to RGB.\n", |
| 1436 | + " Follow these steps:\n", |
| 1437 | + " 1. It is very hard to change saturation in standard RGB format because the colors are mixed. \n", |
| 1438 | + " First, convert the image to HSV format (`cv2.COLOR_RGB2HSV`) format using: `hsv_img = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)`.\n", |
| 1439 | + " 2. Now that the image is in HSV, you can isolate the Saturation. Split the image into its three separate channels using: `cv2.split(hsv_img)`.\n", |
| 1440 | + " 3. Multiply the saturation channel by the `saturation_factor`. \n", |
| 1441 | + " 4. Pixel values cannot go above 255 or below 0. Use NumPy's clip function (`np.clip`) to enforce this limit.\n", |
| 1442 | + " 5. Math operations can change the data type. Force the new saturation channel back into standard image format `uint8`.\n", |
| 1443 | + " 6. Put the three channels back together in the correct order using `cv2.merge()`.\n", |
| 1444 | + " 7. Finally, convert the image back to normal RGB format (`cv2.COLOR_HSV2RGB`).\n", |
| 1445 | + "\n", |
| 1446 | + " Args:\n", |
| 1447 | + " img (np.ndarray): The input image in RGB format.\n", |
| 1448 | + " saturation_factor (float): The multiplier for the saturation channel. Values greater than 1 increase saturation, while values between 0 and 1 decrease it.\n", |
| 1449 | + "\n", |
| 1450 | + " Returns:\n", |
| 1451 | + " np.ndarray: The saturation-adjusted image.\n", |
| 1452 | + " \"\"\"\n", |
1278 | 1453 | " # Start your code here\n", |
1279 | 1454 | " return\n", |
1280 | 1455 | " # End your code here" |
|
0 commit comments