diff --git a/skimage/measure/profile.py b/skimage/measure/profile.py index 98038e06..7cb6a135 100644 --- a/skimage/measure/profile.py +++ b/skimage/measure/profile.py @@ -50,6 +50,43 @@ def profile_line(img, src, dst, linewidth=1, The destination point is included in the profile, in contrast to standard numpy indexing. """ + perp_lines = _line_profile_coordinates(src, dst, linewidth=linewidth) + if img.ndim == 3: + pixels = [nd.map_coordinates(img[..., i], perp_lines, + order=order, mode=mode, cval=cval) + for i in range(img.shape[2])] + pixels = np.transpose(np.asarray(pixels), (1, 2, 0)) + else: + pixels = nd.map_coordinates(img, perp_lines, + order=order, mode=mode, cval=cval) + intensities = pixels.mean(axis=1) + + return intensities + + +def _line_profile_coordinates(src, dst, linewidth=1): + """Return the coordinates of the profile of an image along a scan line. + + Parameters + ---------- + src : 2-tuple of numeric scalar (float or int) + The start point of the scan line. + dst : 2-tuple of numeric scalar (float or int) + The end point of the scan line. + linewidth : int, optional + Width of the scan, perpendicular to the line + + Returns + ------- + return_value : array + The coordinates of the profile along the scan line. The length of the profile is the ceil of the computed length of the scan line. The array is of the form (2, length, linewidth). + + Notes + ----- + This is a utility method meant to be used internally by skimage functions. + The destination point is included in the profile, in contrast to + standard numpy indexing. + """ src_row, src_col = src = np.asarray(src, dtype=float) dst_row, dst_col = dst = np.asarray(dst, dtype=float) d_row, d_col = dst - src @@ -70,18 +107,5 @@ def profile_line(img, src, dst, linewidth=1, linewidth) for row_i in line_row]) perp_cols = np.array([np.linspace(col_i - col_width, col_i + col_width, linewidth) for col_i in line_col]) - perp_lines = np.array([perp_rows, perp_cols]) - - if img.ndim == 3: - pixels = [nd.map_coordinates(img[..., i], perp_lines, - order=order, mode=mode, cval=cval) - for i in range(img.shape[2])] - pixels = np.transpose(np.asarray(pixels), (1, 2, 0)) - else: - pixels = nd.map_coordinates(img, perp_lines, - order=order, mode=mode, cval=cval) - - intensities = pixels.mean(axis=1) - - return intensities + return np.array([perp_rows, perp_cols]) diff --git a/skimage/viewer/plugins/lineprofile.py b/skimage/viewer/plugins/lineprofile.py index 03d83277..03b5eaaf 100644 --- a/skimage/viewer/plugins/lineprofile.py +++ b/skimage/viewer/plugins/lineprofile.py @@ -152,23 +152,16 @@ class LineProfile(PlotPlugin): scan : (P,) or (P, 3) array of int or float The line scan values across the image. """ - (x1, y1), (x2, y2) = self.line_tool.end_points + end_points = self.line_tool.end_points line_image = np.zeros(self.image_viewer.original_image.shape[:2], np.uint8) - w = self.line_tool.linewidth - if w > 1: - a = np.arctan2((y2 - y1), (x2 - x1)) - xinc = abs(int(np.cos(a) * w / 2)) + 1 - yinc = abs(int(np.sin(a) * w / 2)) + 1 - if ((x2 > x1) and (y2 > y1)) or ((x2 < x1) and (y2 < y1)): - xinc *= -1 - xx = np.array([x1 + xinc, x1 - xinc, x2 - xinc, x2 + xinc], - dtype=np.int) - yy = np.array([y1 + yinc, y1 - yinc, y2 - yinc, y2 + yinc], - dtype=np.int) - rrp, ccp = draw.polygon(yy, xx, line_image.shape) - line_image[rrp, ccp] = 128 - rr, cc = draw.line(int(y1), int(x1), int(y2), int(x2)) + width = self.line_tool.linewidth + if width > 1: + rp, cp = measure.profile._line_profile_coordinates( + *end_points[:, ::-1], linewidth=width) + line_image[rp.astype(int), cp.astype(int)] = 128 + (x1, y1), (x2, y2) = end_points.astype(int) + rr, cc = draw.line(y1, x1, y2, x2) line_image[rr, cc] = 255 return line_image, self.scan_data