Ejemplo n.º 1
0
    def update_brightcont(self):
        # The algorithm is by Werner D. Streidt
        # (http://visca.com/ffactory/archives/5-99/msg00021.html)

        if self.contrast > 0:
            delta = 127. * self.contrast / 100
            a = 255. / (255. - delta * 2)
            b = a * (self.brightness - delta)
        else:
            delta = -128. * self.contrast / 100
            a = (256. - delta * 2) / 255.
            b = a * self.brightness + delta

        cv.ConvertScale(self.src_image, self.dst_image, a, b)
        cv.ShowImage("image", self.dst_image)

        cv.CalcArrHist([self.dst_image], self.hist)
        (min_value, max_value, _, _) = cv.GetMinMaxHistValue(self.hist)
        cv.Scale(self.hist.bins, self.hist.bins, float(self.hist_image.height) / max_value, 0)

        cv.Set(self.hist_image, cv.ScalarAll(255))
        bin_w = round(float(self.hist_image.width) / hist_size)

        for i in range(hist_size):
            cv.Rectangle(self.hist_image, (int(i * bin_w), self.hist_image.height),
                         (int((i + 1) * bin_w), self.hist_image.height - cv.Round(self.hist.bins[i])),
                         cv.ScalarAll(0), -1, 8, 0)
       
        cv.ShowImage("histogram", self.hist_image)
Ejemplo n.º 2
0
 def __init__(self, iplimage):
     # Rough-n-ready but it works dammit
     # alpha = cv.CreateMat(iplimage.height,iplimage.width, cv.CV_8UC1)
     alpha = np.zeros((iplimage.shape[0], iplimage.shape[1]), np.uint8)
     # Zieht ein schwarzes Rechteck ueber das Bild
     cv2.rectangle(alpha, (0, 0), (iplimage.shape[1], iplimage.shape[0]),
                   cv.ScalarAll(255), -1)
     rgba = np.zeros((iplimage.shape[0], iplimage.shape[1], 4), np.uint8)
     #cv2.Set(rgba, (1, 2, 3, 4))
     cv2.mixChannels(
         [iplimage, alpha],
         [rgba],
         [
             0,
             0,  # rgba[0] -> bgr[2]
             1,
             1,  # rgba[1] -> bgr[1]
             2,
             2,  # rgba[2] -> bgr[0]
             3,
             3  # rgba[3] -> alpha[0]
         ])
     self.__imagedata = rgba.tostring()
     super(IplQImage,
           self).__init__(self.__imagedata, iplimage.shape[1],
                          iplimage.shape[0], QtGui.QImage.Format_RGB32)
Ejemplo n.º 3
0
def on_trackbar(edge_thresh):

    cv.Threshold(gray, edge, float(edge_thresh), float(edge_thresh),
                 cv.CV_THRESH_BINARY)
    #Distance transform
    cv.DistTransform(edge, dist, cv.CV_DIST_L2, cv.CV_DIST_MASK_5)

    cv.ConvertScale(dist, dist, 5000.0, 0)
    cv.Pow(dist, dist, 0.5)

    cv.ConvertScale(dist, dist32s, 1.0, 0.5)
    cv.AndS(dist32s, cv.ScalarAll(255), dist32s, None)
    cv.ConvertScale(dist32s, dist8u1, 1, 0)
    cv.ConvertScale(dist32s, dist32s, -1, 0)
    cv.AddS(dist32s, cv.ScalarAll(255), dist32s, None)
    cv.ConvertScale(dist32s, dist8u2, 1, 0)
    cv.Merge(dist8u1, dist8u2, dist8u2, None, dist8u)
    cv.ShowImage(wndname, dist8u)
Ejemplo n.º 4
0
 def get_working(self):
     (width, height) = cv.GetSize(self.image)
     dest = cv.CreateMat(height, width, cv.CV_8UC3)
     mask8x1 = cv.CreateImage(cv.GetSize(self.image), 8, 1)
     cv.Zero(mask8x1)
     cv.FillConvexPoly(mask8x1, self.cur_contour, cv.ScalarAll(255))
     # Could 8x3 mask copy but histogram mask will take care of it
     cv.Copy(self.image, dest)
     return (dest, mask8x1)
Ejemplo n.º 5
0
 def on_mouse(self, event, x, y, flags, param):
     pt = (x, y)
     if event == cv.CV_EVENT_LBUTTONUP or not (flags & cv.CV_EVENT_FLAG_LBUTTON):
         self.prev_pt = None
     elif event == cv.CV_EVENT_LBUTTONDOWN:
         self.prev_pt = pt
     elif event == cv.CV_EVENT_MOUSEMOVE and (flags & cv.CV_EVENT_FLAG_LBUTTON) :
         if self.prev_pt:
             for dst in self.dests:
                 cv.Line(dst, self.prev_pt, pt, cv.ScalarAll(255), 5, 8, 0)
         self.prev_pt = pt
         cv.ShowImage(self.windowname, img)
Ejemplo n.º 6
0
    def compute_ref(self):
        '''Compute a reference histogram that matched regions should approximate'''
        (image, polygon) = self.get_ref()

        (width, height) = cv.GetSize(image)
        # (rows, cols,...)
        dest = cv.CreateMat(height, width, cv.CV_8UC3)
        mask8x1 = cv.CreateImage(cv.GetSize(image), 8, 1)
        cv.Zero(mask8x1)
        cv.FillConvexPoly(mask8x1, polygon, cv.ScalarAll(255))
        cv.Copy(image, dest)

        self.ref_hist = hs_histogram(dest, mask8x1)
Ejemplo n.º 7
0
 def on_mouse(self, event, x, y, flags, param):
     pt = (x, y)
     if event == cv.CV_EVENT_LBUTTONUP or not (flags
                                               & cv.CV_EVENT_FLAG_LBUTTON):
         self.prev_pt = None
     elif event == cv.CV_EVENT_LBUTTONDOWN:
         self.prev_pt = pt
     elif event == cv.CV_EVENT_MOUSEMOVE and (flags
                                              & cv.CV_EVENT_FLAG_LBUTTON):
         if self.prev_pt:
             for im in [self.image] + self.chans:
                 cv.Line(im, self.prev_pt, pt, cv.ScalarAll(255), 5, 8, 0)
         self.prev_pt = pt
         cv.ShowImage("image", self.image)
         cv.ShowImage("LSB", self.chans[0])
Ejemplo n.º 8
0
 def draw_contour(self, contour, color=None):
     gray_img = cv.CreateImage(cv.GetSize(self.image), 8, 1)
     #cv.CvtColor( self.image, gray_img, cv.CV_BGR2GRAY )
     cv.Zero(gray_img)
     # DrawContours(img, contour, external_color, hole_color, max_level [, thickness [, lineType [, offset]]]) -> None
     # in my small example external didn't get value but internal did
     #  void cvPolyLine(CvArr* img,
     #        CvPoint** pts, int* npts,
     #        int contours, int is_closed, CvScalar color,
     #        int thickness=1, int lineType=8, int shift=0)
     if color is None:
         # White for default black background
         #color = cv.CV_RGB(255, 255, 255)
         color = cv.ScalarAll(255)
     cv.PolyLine(gray_img, [contour], True, color)
     cv.ShowImage("Contours", gray_img)
     cv.WaitKey()
Ejemplo n.º 9
0
 def __init__(self, iplimage):
     # Rough-n-ready but it works dammit
     alpha = cv.CreateMat(iplimage.height, iplimage.width, cv.CV_8UC1)
     cv.Rectangle(alpha, (0, 0), (iplimage.width, iplimage.height),
                  cv.ScalarAll(255), -1)
     rgba = cv.CreateMat(iplimage.height, iplimage.width, cv.CV_8UC4)
     cv.Set(rgba, (1, 2, 3, 4))
     cv.MixChannels(
         [iplimage, alpha],
         [rgba],
         [
             (0, 0),  # rgba[0] -> bgr[2]
             (1, 1),  # rgba[1] -> bgr[1]
             (2, 2),  # rgba[2] -> bgr[0]
             (3, 3)  # rgba[3] -> alpha[0]
         ])
     self.__imagedata = rgba.tostring()
     super(IplQImage, self).__init__(self.__imagedata, iplimage.width,
                                     iplimage.height, QImage.Format_RGB32)
Ejemplo n.º 10
0
    def undistort(self, img):
        # intrinsic parameters
        fx = self.C[0, 0]
        fy = self.C[1, 1]
        cx = self.C[0, 2]
        cy = self.C[1, 2]
        # radial distortion coefficients
        k1, k2 = self.dist[0:2]
        # †angential distortion coefficients
        p1, p2 = self.dist[2:4]
        intrinsics = cv.CreateMat(3, 3, cv.CV_64FC1)
        cv.Zero(intrinsics)
        intrinsics[0, 0] = float(fx)
        intrinsics[1, 1] = float(fy)
        intrinsics[2, 2] = 1.0
        intrinsics[0, 2] = float(cx)
        intrinsics[1, 2] = float(cy)

        dist_coeffs = cv.CreateMat(1, 4, cv.CV_64FC1)
        cv.Zero(dist_coeffs)
        dist_coeffs[0, 0] = float(k1)
        dist_coeffs[0, 1] = float(k2)
        dist_coeffs[0, 2] = float(p1)
        dist_coeffs[0, 3] = float(p2)

        #src = cv.LoadImage(src)
        src = cv.fromarray(img)
        dst = img.copy()
        dst = cv.fromarray(dst)
        #dst = cv.CreateImage(cv.GetSize(src), src.type, src.channels)
        mapx = cv.CreateImage(cv.GetSize(src), cv.IPL_DEPTH_32F, 1)
        mapy = cv.CreateImage(cv.GetSize(src), cv.IPL_DEPTH_32F, 1)
        cv.InitUndistortMap(intrinsics, dist_coeffs, mapx, mapy)
        cv.Remap(src, dst, mapx,
                 mapy, cv.CV_INTER_LINEAR + cv.CV_WARP_FILL_OUTLIERS,
                 cv.ScalarAll(0))
        # cv.Undistort2(src,dst, intrinsics, dist_coeffs)
        return np.array(dst)
Ejemplo n.º 11
0
        if c == ord('w'):
            storage = cv.CreateMemStorage(0)
            #cv.SaveImage("wshed_mask.png", marker_mask)
            #marker_mask = cv.LoadImage("wshed_mask.png", 0)
            contours = cv.FindContours(marker_mask, storage, cv.CV_RETR_CCOMP,
                                       cv.CV_CHAIN_APPROX_SIMPLE)

            def contour_iterator(contour):
                while contour:
                    yield contour
                    contour = contour.h_next()

            cv.Zero(markers)
            comp_count = 0
            for c in contour_iterator(contours):
                cv.DrawContours(markers, c, cv.ScalarAll(comp_count + 1),
                                cv.ScalarAll(comp_count + 1), -1, -1, 8)
                comp_count += 1

            cv.Watershed(img0, markers)

            cv.Set(wshed, cv.ScalarAll(255))

            # paint the watershed image
            color_tab = [
                (cv.RandInt(rng) % 180 + 50, cv.RandInt(rng) % 180 + 50,
                 cv.RandInt(rng) % 180 + 50) for i in range(comp_count)
            ]
            for j in range(markers.height):
                for i in range(markers.width):
                    idx = markers[j, i]
Ejemplo n.º 12
0
        if c == ord('w'):
            storage = cv.CreateMemStorage(0)
            #cv.SaveImage("wshed_mask.png", marker_mask)
            #marker_mask = cv.LoadImage("wshed_mask.png", 0)
            contours = cv.FindContours(marker_mask, storage, cv.CV_RETR_CCOMP, cv.CV_CHAIN_APPROX_SIMPLE)
            def contour_iterator(contour):
                while contour:
                    yield contour
                    contour = contour.h_next()

            cv.Zero(markers)
            comp_count = 0
            for c in contour_iterator(contours):
                cv.DrawContours(markers,
                                c,
                                cv.ScalarAll(comp_count + 1),
                                cv.ScalarAll(comp_count + 1),
                                -1,
                                -1,
                                8)
                comp_count += 1

            cv.Watershed(img0, markers)

            cv.Set(wshed, cv.ScalarAll(255))

            # paint the watershed image
            color_tab = [(cv.RandInt(rng) % 180 + 50, cv.RandInt(rng) % 180 + 50, cv.RandInt(rng) % 180 + 50) for i in range(comp_count)]
            for j in range(markers.height):
                for i in range(markers.width):
                    idx = markers[j, i]
Ejemplo n.º 13
0
    cv.DFT( dft_A, dft_A, cv.CV_DXT_FORWARD, complexInput.height )

    cv.NamedWindow("win", 0)
    cv.NamedWindow("magnitude", 0)
    cv.ShowImage("win", im)

    # Split Fourier in real and imaginary parts
    cv.Split( dft_A, image_Re, image_Im, None, None )

    # Compute the magnitude of the spectrum Mag = sqrt(Re^2 + Im^2)
    cv.Pow( image_Re, image_Re, 2.0)
    cv.Pow( image_Im, image_Im, 2.0)
    cv.Add( image_Re, image_Im, image_Re, None)
    cv.Pow( image_Re, image_Re, 0.5 )

    # Compute log(1 + Mag)
    cv.AddS( image_Re, cv.ScalarAll(1.0), image_Re, None ) # 1 + Mag
    cv.Log( image_Re, image_Re ) # log(1 + Mag)


    # Rearrange the quadrants of Fourier image so that the origin is at
    # the image center
    cvShiftDFT( image_Re, image_Re )

    min, max, pt1, pt2 = cv.MinMaxLoc(image_Re)
    cv.Scale(image_Re, image_Re, 1.0/(max-min), 1.0*(-min)/(max-min))
    cv.ShowImage("magnitude", image_Re)

    cv.WaitKey(0)
    cv.DestroyAllWindows()
Ejemplo n.º 14
0
        font = cv.InitFont(random.randrange(0, 8),
                           random.randrange(0, 100) * 0.05 + 0.01,
                           random.randrange(0, 100) * 0.05 + 0.01,
                           random.randrange(0, 5) * 0.1,
                           random.randrange(0, 10), line_type)

        cv.PutText(image, "Testing text rendering!", pt1, font,
                   random_color(random))

        cv.ShowImage(window_name, image)
        cv.WaitKey(delay)

    # prepare a text, and get it's properties
    font = cv.InitFont(cv.CV_FONT_HERSHEY_COMPLEX, 3, 3, 0.0, 5, line_type)
    text_size, ymin = cv.GetTextSize("OpenCV forever!", font)
    pt1 = ((width - text_size[0]) / 2, (height + text_size[1]) / 2)
    image2 = cv.CloneImage(image)

    # now, draw some OpenCV pub ;-)
    for i in range(0, 512, 2):
        cv.SubS(image2, cv.ScalarAll(i), image)
        (r, g, b) = colorsys.hsv_to_rgb((i % 100) / 100., 1, 1)
        cv.PutText(image, "OpenCV forever!", pt1, font,
                   cv.RGB(255 * r, 255 * g, 255 * b))
        cv.ShowImage(window_name, image)
        cv.WaitKey(delay)

    # wait some key to end
    cv.WaitKey(0)
    cv.DestroyAllWindows()