summary refs log tree commit diff stats
path: root/opencv/code/a14.py
diff options
context:
space:
mode:
authorSudipto Mallick <smlckz@termux-alpine>2024-02-10 12:49:55 +0000
committerSudipto Mallick <smlckz@termux-alpine>2024-02-10 12:49:55 +0000
commit4182a108a01df3aa28009716472f0ef291704866 (patch)
tree8fabeb2d6ddff80b930c02b3d535b5447bbd41dc /opencv/code/a14.py
parent02884d29e4f5aea71364a203dcaecd53600d8aa4 (diff)
downloadzadania-main.tar.gz
Complete DIP assignments main
Diffstat (limited to 'opencv/code/a14.py')
-rw-r--r--opencv/code/a14.py53
1 files changed, 53 insertions, 0 deletions
diff --git a/opencv/code/a14.py b/opencv/code/a14.py
new file mode 100644
index 0000000..ec04f27
--- /dev/null
+++ b/opencv/code/a14.py
@@ -0,0 +1,53 @@
+from matplotlib import pyplot as plt
+import cv2
+import numpy as np
+
+img = cv2.imread("1i.png", 0)
+laplacian = cv2.Laplacian(img, -1, None, 3)
+
+robert_x_kernel = np.array([[-1, 0], [0, 1]])
+robert_y_kernel = np.array([[0, -1], [1, 0]])
+robert_x = cv2.filter2D(img, -1, robert_x_kernel)
+robert_y = cv2.filter2D(img, -1, robert_y_kernel)
+robert_combined = cv2.bitwise_or(robert_x, robert_y)
+
+prewitt_x_kernel = np.array([[-1, -1, -1], [0, 0, 0], [1, 1, 1]])
+prewitt_y_kernel = np.array([[-1, 0, 1], [-1, 0, 1], [-1, 0, 1]])
+prewitt_x = cv2.filter2D(img, -1, prewitt_x_kernel)
+prewitt_y = cv2.filter2D(img, -1, prewitt_y_kernel)
+prewitt_combined = cv2.bitwise_or(prewitt_x, prewitt_y)
+
+sx = cv2.Sobel(img, cv2.CV_64F, 1, 0)
+sy = cv2.Sobel(img, cv2.CV_64F, 0, 1)
+sobel_x = np.uint8(np.absolute(sx))
+sobel_y = np.uint8(np.absolute(sy))
+sobel_combined = cv2.bitwise_or(sobel_x, sobel_y)
+
+empty = np.zeros(img.shape, dtype=np.uint8)
+
+data = [
+    (img, "Original Image"),
+    (laplacian, "Laplacian"),
+    (robert_x, "Robert in x direction"),
+    (robert_y, "Robert in y direction"),
+    (robert_combined, "Combined Robert"),
+    (prewitt_x, "Prewitt in x direction"),
+    (prewitt_y, "Prewitt in y direction"),
+    (prewitt_combined, "Combined Prewitt"),
+    (sobel_x, "Sobel in x direction"),
+    (sobel_y, "Sobel in y direction"),
+    (sobel_combined, "Combined Sobel"),
+    (empty, ""),
+]
+
+fig, axs = plt.subplots(3, 4, figsize=(10, 7))
+
+for ax, (image, title) in zip(axs.flat, data):
+    if image is not img:
+        image = 255 - image
+    ax.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
+    ax.set_title(title, fontsize=10)
+    ax.axis("off")
+
+plt.tight_layout()
+plt.savefig("14.svg")
fdd } /* Generic.Inserted */ .highlight .go { color: #888888 } /* Generic.Output */ .highlight .gp { color: #555555 } /* Generic.Prompt */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #666666 } /* Generic.Subheading */ .highlight .gt { color: #aa0000 } /* Generic.Traceback */ .highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
#!/usr/bin/env raku
use Test;
use JSON::Fast;
use lib $?FILE.IO.dirname;
use Hamming;
plan 9;

my @test-cases = from-json($=pod.pop.contents).List;
for @test-cases -> %case {
  given %case<expected> {
    when .<error>.so {
      throws-like
        { hamming-distance |%case<input><strand1 strand2> },
        Exception,
        message => /
          'left and right strands must be of equal length'
          || 'Constraint type check failed in binding to parameter'
        /,
        %case<description>;
    }

    default {
      is hamming-distance(|%case<input><strand1 strand2>),
        |%case<expected description>;
    }
  }
}

=head2 Test Cases
=begin code
[
  {
    "description": "empty strands",
    "expected": 0,
    "input": {
      "strand1": "",
      "strand2": ""
    },
    "property": "distance"
  },
  {
    "description": "single letter identical strands",
    "expected": 0,
    "input": {
      "strand1": "A",
      "strand2": "A"
    },
    "property": "distance"
  },
  {
    "description": "single letter different strands",
    "expected": 1,
    "input": {
      "strand1": "G",
      "strand2": "T"
    },
    "property": "distance"
  },
  {
    "description": "long identical strands",
    "expected": 0,
    "input": {
      "strand1": "GGACTGAAATCTG",
      "strand2": "GGACTGAAATCTG"
    },
    "property": "distance"
  },
  {
    "description": "long different strands",
    "expected": 9,
    "input": {
      "strand1": "GGACGGATTCTG",
      "strand2": "AGGACGGATTCT"
    },
    "property": "distance"
  },
  {
    "description": "disallow first strand longer",
    "expected": {
      "error": "left and right strands must be of equal length"
    },
    "input": {
      "strand1": "AATG",
      "strand2": "AAA"
    },
    "property": "distance"
  },
  {
    "description": "disallow second strand longer",
    "expected": {
      "error": "left and right strands must be of equal length"
    },
    "input": {
      "strand1": "ATA",
      "strand2": "AGTG"
    },
    "property": "distance"
  },
  {
    "description": "disallow left empty strand",
    "expected": {
      "error": "left strand must not be empty"
    },
    "input": {
      "strand1": "",
      "strand2": "G"
    },
    "property": "distance"
  },
  {
    "description": "disallow right empty strand",
    "expected": {
      "error": "right strand must not be empty"
    },
    "input": {
      "strand1": "G",
      "strand2": ""
    },
    "property": "distance"
  }
]
=end code