segments_* and segments.gen files in Lucene

From Book: LuceneInAction2ndEdition

 

-rw-rw-rw- 1 mike users 12327579 Feb 29 05:29 _2.fdt
-rw-rw-rw- 1 mike users 6400 Feb 29 05:29 _2.fdx
-rw-rw-rw- 1 mike users 33 Feb 29 05:29 _2.fnm
-rw-rw-rw- 1 mike users 1036074 Feb 29 05:29 _2.frq
-rw-rw-rw- 1 mike users 2404 Feb 29 05:29 _2.nrm
-rw-rw-rw- 1 mike users 2128366 Feb 29 05:29 _2.prx
-rw-rw-rw- 1 mike users 14055 Feb 29 05:29 _2.tii
-rw-rw-rw- 1 mike users 1034353 Feb 29 05:29 _2.tis
-rw-rw-rw- 1 mike users 5829 Feb 29 05:29 _2.tvd
-rw-rw-rw- 1 mike users 10227627 Feb 29 05:29 _2.tvf
-rw-rw-rw- 1 mike users 12804 Feb 29 05:29 _2.tvx
-rw-rw-rw- 1 mike users 17 Mar 30 03:34 random.txt
-rw-rw-rw- 1 mike users 20 Feb 29 05:29 segments.gen
-rw-rw-rw- 1 mike users 53 Feb 29 05:29 segments_3


The secret to this is the segments file (segments_3). As you may have guessed from its name, the
segments file stores the name and certain details of all existing index segments
. Every time an
IndexWriter commits a change to the index, the generation (the _3 in the above listing) of the
segments file is incremented. For example, a commit to this index would write segments_4 and remove
segments_3 as well as any now unreferenced files. Before accessing any files in the index directory,
Lucene consults this file to figure out which index files to open and read.
Our example index has a single
segment, _2, whose name is stored in this segments file, so Lucene knows to look only for files with the
_2 prefix. Lucene also limits itself to files with known extensions, such as .fdt, .fdx, and other extensions
shown in our example, so even saving a file with a segment prefix, such as _2.txt, won’t throw Lucene off.
Of course, polluting an index directory with non-Lucene files is strongly discouraged.


The exact number of files that constitute a Lucene index and each segment varies from index to index
and depends on the number of fields the index contains. However, every index contains a single segments
file and a single segments.gen file. The segments.gen file is always 20 bytes and contains the suffix
(generation) of the current segments as a redundant way for Lucene to determine the most recent
commit
.

 

 

Note: if we lost segments.gen file, we are not able to tell the most recent last commit.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import cv2 import numpy as np import torch import torch.nn.functional as F from skimage.segmentation import slic import matplotlib.pyplot as plt from skimage.segmentation import mark_boundaries from skimage import img_as_float # 定义超像素数量 num_segments = 100 # 加载图像 A 和 B img_a = cv2.imread('img_a.jpg') img_b = cv2.imread('img_b.jpg') # 对图像 A 进行超像素分割,并获取每个超像素块的像素范围 segments_a = slic(img_as_float(img_a), n_segments=num_segments, sigma=5) pixel_ranges = [] for i in range(num_segments): mask = (segments_a == i) indices = np.where(mask)[1] pixel_range = (np.min(indices), np.max(indices)) pixel_ranges.append(pixel_range) # 将像素范围应用到图像 B 上实现超像素分割 segments_b = np.zeros_like(segments_a) for i in range(num_segments): pixel_range = pixel_ranges[i] segment_b = img_b[:, pixel_range[0]:pixel_range[1], :] segment_b = torch.from_numpy(segment_b.transpose(2, 0, 1)).unsqueeze(0).float() segment_b = F.interpolate(segment_b, size=(img_b.shape[0], pixel_range[1] - pixel_range[0]), mode='bilinear', align_corners=True) segment_b = segment_b.squeeze(0).numpy().transpose(1, 2, 0).astype(np.uint8) gray = cv2.cvtColor(segment_b, cv2.COLOR_BGR2GRAY) _, mask = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY) segments_b[np.where(mask)] = i # 可视化超像素分割结果 fig = plt.figure('Superpixels') ax = fig.add_subplot(1, 2, 1) ax.imshow(mark_boundaries(img_as_float(cv2.cvtColor(img_a, cv2.COLOR_BGR2RGB)), segments_a)) ax = fig.add_subplot(1, 2, 2) ax.imshow(mark_boundaries(img_as_float(cv2.cvtColor(img_b, cv2.COLOR_BGR2RGB)), segments_b)) plt.axis("off") plt.show(),上述代码中segments_a = slic(img_as_float(img_a), n_segments=num_segments, sigma=5)出现错误:ValueError: Cannot convert from object to float64.
05-30
这个错误的原因可能是因为 `img_a` 的类型不是浮点类型,需要将其转换为浮点类型后再进行超像素分割。可以使用 `img_as_float` 函数将其转换为浮点类型,代码如下: ``` import cv2 import numpy as np import torch import torch.nn.functional as F from skimage.segmentation import slic import matplotlib.pyplot as plt from skimage.segmentation import mark_boundaries from skimage import img_as_float # 定义超像素数量 num_segments = 100 # 加载图像 A 和 B img_a = cv2.imread('img_a.jpg') img_b = cv2.imread('img_b.jpg') # 将图像 A 转换为浮点类型 img_a = img_as_float(img_a) # 对图像 A 进行超像素分割,并获取每个超像素块的像素范围 segments_a = slic(img_a, n_segments=num_segments, sigma=5) pixel_ranges = [] for i in range(num_segments): mask = (segments_a == i) indices = np.where(mask)[1] pixel_range = (np.min(indices), np.max(indices)) pixel_ranges.append(pixel_range) # 将像素范围应用到图像 B 上实现超像素分割 segments_b = np.zeros_like(segments_a) for i in range(num_segments): pixel_range = pixel_ranges[i] segment_b = img_b[:, pixel_range[0]:pixel_range[1], :] segment_b = torch.from_numpy(segment_b.transpose(2, 0, 1)).unsqueeze(0).float() segment_b = F.interpolate(segment_b, size=(img_b.shape[0], pixel_range[1] - pixel_range[0]), mode='bilinear', align_corners=True) segment_b = segment_b.squeeze(0).numpy().transpose(1, 2, 0).astype(np.uint8) gray = cv2.cvtColor(segment_b, cv2.COLOR_BGR2GRAY) _, mask = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY) segments_b[np.where(mask)] = i # 可视化超像素分割结果 fig = plt.figure('Superpixels') ax = fig.add_subplot(1, 2, 1) ax.imshow(mark_boundaries(img_as_float(cv2.cvtColor(img_a, cv2.COLOR_BGR2RGB)), segments_a)) ax = fig.add_subplot(1, 2, 2) ax.imshow(mark_boundaries(img_as_float(cv2.cvtColor(img_b, cv2.COLOR_BGR2RGB)), segments_b)) plt.axis("off") plt.show() ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值