{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "105a13c6", "metadata": {}, "outputs": [], "source": [ "import cv2 as cv\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": 2, "id": "5822e760", "metadata": {}, "outputs": [], "source": [ "img = cv.imread('reflections.jpg')" ] }, { "cell_type": "code", "execution_count": 3, "id": "4291e056", "metadata": {}, "outputs": [], "source": [ "squarechunk = img[100:200,100:200]" ] }, { "cell_type": "code", "execution_count": 4, "id": "a9f0a286", "metadata": {}, "outputs": [], "source": [ "cv.imshow('image', img)\n", "cv.waitKey(0)\n", "cv.destroyAllWindows()" ] }, { "cell_type": "code", "execution_count": 5, "id": "ce561fe7", "metadata": {}, "outputs": [], "source": [ "squarechunk[:,:,2] = 200" ] }, { "cell_type": "code", "execution_count": 6, "id": "adf00927", "metadata": {}, "outputs": [], "source": [ "cv.imshow('image', img)\n", "cv.waitKey(0)\n", "cv.destroyAllWindows()" ] }, { "cell_type": "code", "execution_count": 7, "id": "fe96e18d", "metadata": {}, "outputs": [], "source": [ "cv.imshow('image', squarechunk)\n", "cv.waitKey(0)\n", "cv.destroyAllWindows()" ] }, { "cell_type": "code", "execution_count": 9, "id": "1190f553", "metadata": {}, "outputs": [], "source": [ "blurmask = np.array([[0.1, 0.1, 0.1],[0.1, 0.2, 0.1],[0.1, 0.1, 0.1]])" ] }, { "cell_type": "code", "execution_count": 10, "id": "6c8ce123", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on built-in function filter2D:\n", "\n", "filter2D(...)\n", " filter2D(src, ddepth, kernel[, dst[, anchor[, delta[, borderType]]]]) -> dst\n", " . @brief Convolves an image with the kernel.\n", " .\n", " . The function applies an arbitrary linear filter to an image. In-place operation is supported. When\n", " . the aperture is partially outside the image, the function interpolates outlier pixel values\n", " . according to the specified border mode.\n", " .\n", " . The function does actually compute correlation, not the convolution:\n", " .\n", " . \\f[\\texttt{dst} (x,y) = \\sum _{ \\substack{0\\leq x' < \\texttt{kernel.cols}\\\\{0\\leq y' < \\texttt{kernel.rows}}}} \\texttt{kernel} (x',y')* \\texttt{src} (x+x'- \\texttt{anchor.x} ,y+y'- \\texttt{anchor.y} )\\f]\n", " .\n", " . That is, the kernel is not mirrored around the anchor point. If you need a real convolution, flip\n", " . the kernel using #flip and set the new anchor to `(kernel.cols - anchor.x - 1, kernel.rows -\n", " . anchor.y - 1)`.\n", " .\n", " . The function uses the DFT-based algorithm in case of sufficiently large kernels (~`11 x 11` or\n", " . larger) and the direct algorithm for small kernels.\n", " .\n", " . @param src input image.\n", " . @param dst output image of the same size and the same number of channels as src.\n", " . @param ddepth desired depth of the destination image, see @ref filter_depths \"combinations\"\n", " . @param kernel convolution kernel (or rather a correlation kernel), a single-channel floating point\n", " . matrix; if you want to apply different kernels to different channels, split the image into\n", " . separate color planes using split and process them individually.\n", " . @param anchor anchor of the kernel that indicates the relative position of a filtered point within\n", " . the kernel; the anchor should lie within the kernel; default value (-1,-1) means that the anchor\n", " . is at the kernel center.\n", " . @param delta optional value added to the filtered pixels before storing them in dst.\n", " . @param borderType pixel extrapolation method, see #BorderTypes. #BORDER_WRAP is not supported.\n", " . @sa sepFilter2D, dft, matchTemplate\n", "\n" ] } ], "source": [ "help(cv.filter2D)" ] }, { "cell_type": "code", "execution_count": 11, "id": "3e0b33a9", "metadata": {}, "outputs": [], "source": [ "blurimg = cv.filter2D(img, -1, blurmask)" ] }, { "cell_type": "code", "execution_count": 12, "id": "fffd84f1", "metadata": {}, "outputs": [], "source": [ "cv.imshow('image', blurimg)\n", "cv.waitKey(0)\n", "cv.destroyAllWindows()" ] }, { "cell_type": "code", "execution_count": 13, "id": "81a42880", "metadata": {}, "outputs": [], "source": [ "cv.imshow('image', img)\n", "cv.waitKey(0)\n", "cv.destroyAllWindows()" ] }, { "cell_type": "code", "execution_count": 15, "id": "43197234", "metadata": {}, "outputs": [], "source": [ "blurimg2 = cv.GaussianBlur(img, [11, 11], 4)" ] }, { "cell_type": "code", "execution_count": 16, "id": "e18f6bad", "metadata": {}, "outputs": [], "source": [ "cv.imshow('image', blurimg2)\n", "cv.waitKey(0)\n", "cv.destroyAllWindows()" ] }, { "cell_type": "code", "execution_count": 17, "id": "795ddfd1", "metadata": {}, "outputs": [], "source": [ "smoothmask2 = cv.getGaussianKernel(11,4)" ] }, { "cell_type": "code", "execution_count": 18, "id": "ebd31473", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0.0548838 ],\n", " [0.07270922],\n", " [0.09048808],\n", " [0.10579129],\n", " [0.116189 ],\n", " [0.11987723],\n", " [0.116189 ],\n", " [0.10579129],\n", " [0.09048808],\n", " [0.07270922],\n", " [0.0548838 ]])" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "smoothmask2" ] }, { "cell_type": "code", "execution_count": 19, "id": "a98f63e9", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0.0548838 , 0.07270922, 0.09048808, 0.10579129, 0.116189 ,\n", " 0.11987723, 0.116189 , 0.10579129, 0.09048808, 0.07270922,\n", " 0.0548838 ]])" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "smoothmask2.transpose()" ] }, { "cell_type": "code", "execution_count": 21, "id": "5407db10", "metadata": {}, "outputs": [], "source": [ "smoothmask2 = smoothmask2.transpose() * smoothmask2" ] }, { "cell_type": "code", "execution_count": 22, "id": "a16d6192", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0.00301223, 0.00399056, 0.00496633, 0.00580623, 0.00637689,\n", " 0.00657932, 0.00637689, 0.00580623, 0.00496633, 0.00399056,\n", " 0.00301223],\n", " [0.00399056, 0.00528663, 0.00657932, 0.007692 , 0.00844801,\n", " 0.00871618, 0.00844801, 0.007692 , 0.00657932, 0.00528663,\n", " 0.00399056],\n", " [0.00496633, 0.00657932, 0.00818809, 0.00957285, 0.01051372,\n", " 0.01084746, 0.01051372, 0.00957285, 0.00818809, 0.00657932,\n", " 0.00496633],\n", " [0.00580623, 0.007692 , 0.00957285, 0.0111918 , 0.01229178,\n", " 0.01268197, 0.01229178, 0.0111918 , 0.00957285, 0.007692 ,\n", " 0.00580623],\n", " [0.00637689, 0.00844801, 0.01051372, 0.01229178, 0.01349988,\n", " 0.01392842, 0.01349988, 0.01229178, 0.01051372, 0.00844801,\n", " 0.00637689],\n", " [0.00657932, 0.00871618, 0.01084746, 0.01268197, 0.01392842,\n", " 0.01437055, 0.01392842, 0.01268197, 0.01084746, 0.00871618,\n", " 0.00657932],\n", " [0.00637689, 0.00844801, 0.01051372, 0.01229178, 0.01349988,\n", " 0.01392842, 0.01349988, 0.01229178, 0.01051372, 0.00844801,\n", " 0.00637689],\n", " [0.00580623, 0.007692 , 0.00957285, 0.0111918 , 0.01229178,\n", " 0.01268197, 0.01229178, 0.0111918 , 0.00957285, 0.007692 ,\n", " 0.00580623],\n", " [0.00496633, 0.00657932, 0.00818809, 0.00957285, 0.01051372,\n", " 0.01084746, 0.01051372, 0.00957285, 0.00818809, 0.00657932,\n", " 0.00496633],\n", " [0.00399056, 0.00528663, 0.00657932, 0.007692 , 0.00844801,\n", " 0.00871618, 0.00844801, 0.007692 , 0.00657932, 0.00528663,\n", " 0.00399056],\n", " [0.00301223, 0.00399056, 0.00496633, 0.00580623, 0.00637689,\n", " 0.00657932, 0.00637689, 0.00580623, 0.00496633, 0.00399056,\n", " 0.00301223]])" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "smoothmask2" ] }, { "cell_type": "code", "execution_count": 23, "id": "77574d90", "metadata": {}, "outputs": [], "source": [ "blurimg5 = cv.filter2D(img, -1, smoothmask2)" ] }, { "cell_type": "code", "execution_count": 24, "id": "64d02c63", "metadata": {}, "outputs": [], "source": [ "cv.imshow('image', blurimg5)\n", "cv.waitKey(0)\n", "cv.destroyAllWindows()" ] }, { "cell_type": "code", "execution_count": 25, "id": "d0f804ca", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_keys(['?', 0, 'byte', 'b', 1, 'ubyte', 'B', 2, 'short', 'h', 3, 'ushort', 'H', 4, 'i', 5, 'uint', 'I', 6, 'intp', 'p', 7, 'uintp', 'P', 8, 'long', 'l', 'ulong', 'L', 'longlong', 'q', 9, 'ulonglong', 'Q', 10, 'half', 'e', 23, 'f', 11, 'double', 'd', 12, 'longdouble', 'g', 13, 'cfloat', 'F', 14, 'cdouble', 'D', 15, 'clongdouble', 'G', 16, 'O', 17, 'S', 18, 'unicode', 'U', 19, 'void', 'V', 20, 'M', 21, 'm', 22, 'b1', 'bool8', 'i8', 'int64', 'u8', 'uint64', 'f2', 'float16', 'f4', 'float32', 'f8', 'float64', 'f16', 'float128', 'c8', 'complex64', 'c16', 'complex128', 'c32', 'complex256', 'object0', 'bytes0', 'str0', 'void0', 'M8', 'datetime64', 'm8', 'timedelta64', 'int32', 'i4', 'uint32', 'u4', 'int16', 'i2', 'uint16', 'u2', 'int8', 'i1', 'uint8', 'u1', 'complex_', 'single', 'csingle', 'singlecomplex', 'float_', 'intc', 'uintc', 'int_', 'longfloat', 'clongfloat', 'longcomplex', 'bool_', 'bytes_', 'string_', 'str_', 'unicode_', 'object_', 'int', 'float', 'complex', 'bool', 'object', 'str', 'bytes', 'a', 'int0', 'uint0'])" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.sctypeDict.keys()" ] }, { "cell_type": "code", "execution_count": 26, "id": "4caff419", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'int': [numpy.int8, numpy.int16, numpy.int32, numpy.int64],\n", " 'uint': [numpy.uint8, numpy.uint16, numpy.uint32, numpy.uint64],\n", " 'float': [numpy.float16, numpy.float32, numpy.float64, numpy.longdouble],\n", " 'complex': [numpy.complex64, numpy.complex128, numpy.clongdouble],\n", " 'others': [bool, object, bytes, str, numpy.void]}" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.sctypes" ] }, { "cell_type": "code", "execution_count": 27, "id": "4e3ec613", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on function ones in module numpy:\n", "\n", "ones(shape, dtype=None, order='C', *, like=None)\n", " Return a new array of given shape and type, filled with ones.\n", "\n", " Parameters\n", " ----------\n", " shape : int or sequence of ints\n", " Shape of the new array, e.g., ``(2, 3)`` or ``2``.\n", " dtype : data-type, optional\n", " The desired data-type for the array, e.g., `numpy.int8`. Default is\n", " `numpy.float64`.\n", " order : {'C', 'F'}, optional, default: C\n", " Whether to store multi-dimensional data in row-major\n", " (C-style) or column-major (Fortran-style) order in\n", " memory.\n", " like : array_like, optional\n", " Reference object to allow the creation of arrays which are not\n", " NumPy arrays. If an array-like passed in as ``like`` supports\n", " the ``__array_function__`` protocol, the result will be defined\n", " by it. In this case, it ensures the creation of an array object\n", " compatible with that passed in via this argument.\n", "\n", " .. versionadded:: 1.20.0\n", "\n", " Returns\n", " -------\n", " out : ndarray\n", " Array of ones with the given shape, dtype, and order.\n", "\n", " See Also\n", " --------\n", " ones_like : Return an array of ones with shape and type of input.\n", " empty : Return a new uninitialized array.\n", " zeros : Return a new array setting values to zero.\n", " full : Return a new array of given shape filled with value.\n", "\n", "\n", " Examples\n", " --------\n", " >>> np.ones(5)\n", " array([1., 1., 1., 1., 1.])\n", "\n", " >>> np.ones((5,), dtype=int)\n", " array([1, 1, 1, 1, 1])\n", "\n", " >>> np.ones((2, 1))\n", " array([[1.],\n", " [1.]])\n", "\n", " >>> s = (2,2)\n", " >>> np.ones(s)\n", " array([[1., 1.],\n", " [1., 1.]])\n", "\n" ] } ], "source": [ "help(np.ones)" ] }, { "cell_type": "code", "execution_count": 28, "id": "2f37463f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on built-in function zeros in module numpy:\n", "\n", "zeros(...)\n", " zeros(shape, dtype=float, order='C', *, like=None)\n", "\n", " Return a new array of given shape and type, filled with zeros.\n", "\n", " Parameters\n", " ----------\n", " shape : int or tuple of ints\n", " Shape of the new array, e.g., ``(2, 3)`` or ``2``.\n", " dtype : data-type, optional\n", " The desired data-type for the array, e.g., `numpy.int8`. Default is\n", " `numpy.float64`.\n", " order : {'C', 'F'}, optional, default: 'C'\n", " Whether to store multi-dimensional data in row-major\n", " (C-style) or column-major (Fortran-style) order in\n", " memory.\n", " like : array_like, optional\n", " Reference object to allow the creation of arrays which are not\n", " NumPy arrays. If an array-like passed in as ``like`` supports\n", " the ``__array_function__`` protocol, the result will be defined\n", " by it. In this case, it ensures the creation of an array object\n", " compatible with that passed in via this argument.\n", "\n", " .. versionadded:: 1.20.0\n", "\n", " Returns\n", " -------\n", " out : ndarray\n", " Array of zeros with the given shape, dtype, and order.\n", "\n", " See Also\n", " --------\n", " zeros_like : Return an array of zeros with shape and type of input.\n", " empty : Return a new uninitialized array.\n", " ones : Return a new array setting values to one.\n", " full : Return a new array of given shape filled with value.\n", "\n", " Examples\n", " --------\n", " >>> np.zeros(5)\n", " array([ 0., 0., 0., 0., 0.])\n", "\n", " >>> np.zeros((5,), dtype=int)\n", " array([0, 0, 0, 0, 0])\n", "\n", " >>> np.zeros((2, 1))\n", " array([[ 0.],\n", " [ 0.]])\n", "\n", " >>> s = (2,2)\n", " >>> np.zeros(s)\n", " array([[ 0., 0.],\n", " [ 0., 0.]])\n", "\n", " >>> np.zeros((2,), dtype=[('x', 'i4'), ('y', 'i4')]) # custom dtype\n", " array([(0, 0), (0, 0)],\n", " dtype=[('x', '>> np.empty([2, 2])\n", " array([[ -9.74499359e+001, 6.69583040e-309],\n", " [ 2.13182611e-314, 3.06959433e-309]]) #uninitialized\n", "\n", " >>> np.empty([2, 2], dtype=int)\n", " array([[-1073741821, -1067949133],\n", " [ 496041986, 19249760]]) #uninitialized\n", "\n" ] } ], "source": [ "help(np.empty)" ] }, { "cell_type": "code", "execution_count": 30, "id": "835717a5", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1., 1.],\n", " [1., 1.]], dtype=float32)" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.ones([2,2], np.float32)" ] }, { "cell_type": "code", "execution_count": 31, "id": "3e119213", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0., 0.],\n", " [0., 0.]], dtype=float32)" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.zeros([2,2], np.float32)" ] }, { "cell_type": "code", "execution_count": 32, "id": "9df49a33", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0., 0.],\n", " [0., 0.]], dtype=float32)" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.empty([2,2], np.float32)" ] }, { "cell_type": "code", "execution_count": 33, "id": "14ed7631", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(320, 900, 3)" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "img.shape" ] }, { "cell_type": "code", "execution_count": 34, "id": "810b628e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dtype('uint8')" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "img.dtype" ] }, { "cell_type": "code", "execution_count": 40, "id": "f95f7958", "metadata": {}, "outputs": [], "source": [ "def BGR2YCbCr(bgrimg):\n", " ycbcrimg = np.empty(bgrimg.shape, np.float32)\n", " for r in range(bgrimg.shape[0]):\n", " for c in range(bgrimg.shape[1]):\n", " #ycbcrimg[r,c,0] = bgrimg[r,c,0]*0.114 + bgrimg[r,c,1]*0.587 + bgimg[r,c,2]*0.299\n", " ycbcrimg[r,c,0] = sum(bgrimg[r,c]*np.array([0.114,0.587,0.299]))\n", " ycbcrimg[r,c,1] = 128 + sum(bgimg[r,c] * np.array([0.5, -0.331264,-0.168736]))\n", " ycbcrimg[r,c,2] = 128 + sum(bgimg[r,c] * np.array([-0.08132, -0.418688,0.5]))\n", " return ycbcrimg" ] }, { "cell_type": "code", "execution_count": 36, "id": "4af8c07d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(320, 900, 3)" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "img.shape" ] }, { "cell_type": "code", "execution_count": 38, "id": "b343c1a4", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1.14 , 33.459, 16.445])" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "img[0,0] * np.array([0.114, 0.587, 0.299])\n" ] }, { "cell_type": "code", "execution_count": 39, "id": "06ce2c47", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "51.044" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum(img[0,0] * np.array([0.114, 0.587, 0.299]))" ] }, { "cell_type": "code", "execution_count": null, "id": "757225f1", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.3" } }, "nbformat": 4, "nbformat_minor": 5 }